简体   繁体   中英

Activity of my app goes into background sometimes after I turn off the screen

I am running Camera as a service attached to an activity. There are other services such as upload and Firebase are running along with the camera service. Now my requirement is to keep the service running after I turn off the screen. I am acquiring PARTIAL_WAKE_LOCK too. The services run perfectly for the initial 10 or 12 minutes. After that, the app stops sending any logs to ADB. In the device, the app goes to the background by itself. Not even onPause or onDestroy is logging anything. The logs just stop coming to Android Studio. For resuming the normal functioning I have to manually open the app again,

These are the things that I have already tried,

1.Given permission of acquiring PARTIAL_WAKE_LOCK.

2.Setting android:largeHeap="true"

3.Foreground services

According to Doze mode documentation it ignores wake locks.
Also starting from Android Pie it is impossible to gain camera access from background app.
Also according to android developer guidelines you must free camera, when you app is paused, because it will block all other apps.

onSaveInstanceState function should be called in these situations, check with you and if its called then open the activity again with the delay.

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
            try {
                Intent intent = new Intent(getBaseContext(), YourActivity.class);
                startActivity(intent);
                finish();
            } catch (Exception ex) { }
      }
    }, 200);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM