10

im so confused reading this and trying to figure out if foreground services will run when the device goes into deep doze mode. Could someone clarify. I want to know on marshmallow and upwards if foreground services can continue to run. I always thought ALL THREADs are suspended even foreground services when device sleeps.

I see the doze mode restrictions but nothing about foreground services. im so confused if my services outlives doze mode security settings.

From what i can tell in the doze mode restrictions just network calls are stopped. But lets say i was doing some long running main thread work, it means it can continue to run right ? even in doze mode ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
j2emanue
  • 60,549
  • 65
  • 286
  • 456

2 Answers2

14

Foreground services are not killed in doze mode and it's a great workaround to override the doze mode. Killing the foreground service is highly depending on Mobile OS. Like Huawei, it kills the foreground services after a slack of time and you will not be able to determine the period. Some other phones kill the oldest foreground service if it detects unexpected battery consumption. last year, I spent around 6 months observing phones behavior in killing the foreground services when doze mode is activated or not. and I tried more than one solution to override the doze mode in detecting location every 10 seconds and the best one is the foreground service. So you will face unexpected behavior on some phones but it is the best solution for doze and standby mode. You can see this article and you can also look at this tutorial

lasec0203
  • 2,422
  • 1
  • 21
  • 36
Ramzy Hassan
  • 926
  • 8
  • 21
  • 1
    I need to perform network calls periodically on wear os, for this purpose I developed a simple runnable that posts itself in a thread with a fixed rate and I put it inside a foreground service as a private member, it starts running when startService is called. After 30 minutes the correct behavior stops, calls are not performed but the foreground service remains alive. From that point, every time I turn on the screen, the correct behavior resumes and stops again on screen off. Any ideas? – Andrea Nisticò Jan 03 '20 at 16:04
  • @AndreaNisticò same with me, I'm keeping socket alive on foreground service. Socket connection got disconnected after ~15min device got locked and socket reconnected as device wake up manually or due to some other app's notification(like whatsapp). – Shivam Sharma Feb 13 '20 at 11:33
  • @ShivamSharma I have found a solution: at the beginning of your foreground service acquire a partial wakelock and never release it, moreover, I am launching the foreground service on a separate process. In this way I can run network calls as long as the service lives – Andrea Nisticò Feb 13 '20 at 17:02
  • @AndreaNisticò thank you brother for your response. Can you able to share code snippet exactly what you tried. I'm running a foreground service over a jobScheduler job(which is infinitely running because I never finished that job). And I've also placed a partial wake lock inside onStartCommnad() & never release it. I've started jobscheduler on a separate thread and also I've placed socket part of foreground service inside separate thread. I'm testing on redmi(oreo) ,oneplus(pie). I've monitored behaviour with and without terminal commands. – Shivam Sharma Feb 14 '20 at 07:27
  • @AndreaNisticò I've an VoIP calling SDK module in which I'm running this foreground service over jobscheduler. I've tried to mention foreground service as a process in Manifest file but code inside service is running. But once socket is connected I'm returning an success value inside callback event to app module(i.e. default process). So, separate process is not communicating to default process in this experiment. Also I'm not able to debug the code inside foreground service though sticky notification is generating. Can you please help me out??? – Shivam Sharma Feb 14 '20 at 11:19
  • @ShivamSharma does your foreground service need to communicate with the main app? If you are acquiring a wakelock and it is still not working as expected, try to put the foreground service inside a separate proce, be aware that you will have to implement AIDL in order to communicate with this process. You can define the process inside manifest, under the service definition put – Andrea Nisticò Feb 14 '20 at 15:14
  • 2
    On Pixel 4 XL, foreground service gets killed in Doze Mode – IgorGanapolsky Apr 10 '20 at 17:49
  • 2
    @IgorGanapolsky even with Battery Optimization turned off for the app? – lasec0203 Sep 23 '20 at 20:39
  • 1
    @lasec0203 Yes. The only workaround I found is to acquire partial wake lock. – IgorGanapolsky Sep 24 '20 at 14:04
  • Hi there, Are you guys aware of any way to know if Android kills the foreground service? I have a requirement to notify the backend server if this happens. – ledragon Apr 08 '21 at 07:42
6

Doze mode is for saving your battery. You should put your app in white list For deactivate doze mode.

Source : https://developer.android.com/training/monitoring-device-state/doze-standby

Support for other use cases Almost all apps should be able to support Doze by managing network connectivity, alarms, jobs, and syncs properly, and by using FCM high-priority messages. For a narrow set of use cases, this might not be sufficient. For such cases, the system provides a configurable whitelist of apps that are partially exempt from Doze and App Standby optimizations.

An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions

still apply to the whitelisted app, just as they do to other apps. For example, the whitelisted app’s jobs and syncs are deferred (on API level 23 and below), and its regular AlarmManager alarms do not fire. An app can check whether it is currently on the exemption whitelist by calling isIgnoringBatteryOptimizations().

And Here how to insert your app to white list : 1. Step --> Add this permission in your xml file.

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

2.Step İgnore battery optimizations

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    if (!pm.isIgnoringBatteryOptimizations(packageName)) {
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    }
}