12

Is it possible to use network access when doze is active? If my app is ignoring battery optimization doesn't go in standby but it's affected by doze anyway. Am I missing anything?

greywolf82
  • 21,813
  • 18
  • 54
  • 108

2 Answers2

10

Network access is disabled in doze mode, regardless if your application is ignoring battery optimizations. The only way to wake-up your device from doze mode and to get network access is by sending a high priority Google Cloud Message to your application.

Edit: it is possible to let Android ignore battery optimization for your application, which effectively disables doze mode for your application. However, this requires using the method setExactAndAllowWhileIdle (maximum one wakeup each 15 minutes) and user interaction, which can be done like this:

Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
//battery optimizations toggle
if (pm.isIgnoringBatteryOptimizations(packageName))
    //give the user the option to enable battery optimizations again
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

Edit: it was suggested to add the permission android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to the manifest. Besides that this is unnecessary for the suggested solution, it will result your app to be removed from the Play store.

xnumad
  • 5
  • 3
M66B
  • 1,164
  • 1
  • 8
  • 16
  • 2
    "...which effectively disables doze mode for your application" - No, unfortunately this is not the case. Whitelisting your app for 'ignore battery optimization' does not affect its behavior in doze mode. see here: https://code.google.com/p/android-developer-preview/issues/detail?id=2225#c11 – sec_aw Sep 08 '15 at 11:16
  • When using the alarm manager method _setExactAndAllowWhileIdle_ and disabling battery optimization alarms will go off as expected. – M66B Sep 08 '15 at 13:05
  • setExactAndAllowWhileIdle will work the same in Doze mode, whether 'ignore optimizations' is enabled or not (maybe you're talking about 'app standby' which is another issue). The option 'ignore optimizations' is absolutely misleading, as it does not prevent an app from being affected by Doze mode. See the link in my first comment, which is a statement by a member of the development team (Quote: "...putting an app on the whitelist does not change the behavior for them"). For more info follow the discussion there. – sec_aw Sep 08 '15 at 18:30
  • In my tests there was a difference when using *setExact* and *setExactAndAllowWhileIdle* with battery optimization disabled. *setExactAndAllowWhileIdle* will definitely wake up the device when in doze mode. I am aware this is not what the Google documentation says, but it works with Android M preview 3. – M66B Sep 09 '15 at 05:25
  • Well that's another topic again. Sure there's a difference between setExact and setExactAndAllowWhileIdle, because they are different functions. Only setExactAndAllowWhileIdle will fire in Doze (idle) mode, but the tasks you may do with it seem to be extremely limited (test indicate, this is including some network access), see here: https://code.google.com/p/android-developer-preview/issues/detail?id=2225#c38. – sec_aw Sep 09 '15 at 09:41
  • 2
    Note that *setExactAndAllowWhileIdle* will not fire an alarm when in doze mode when the battery optimizations are enabled. Using *setExactAndAllowWhileIdle* and disabling battery optimization was for me the only way to get network access in doze mode. – M66B Sep 09 '15 at 12:29
  • Well, this is not the documented or intended behavior, but indeed there seem to be strange results as is also to be seen in above linked issue comment. – sec_aw Sep 09 '15 at 14:39
  • @sec_aw is right, remove the edit from the response because is wrong – greywolf82 Sep 13 '15 at 13:02
  • @greywolf82 did you test this? I did and it works as described in the edit using Android M preview 3. – M66B Sep 14 '15 at 06:46
  • 3
    setExactAndAllowWhileIdle wakes up the device as expected but only 1 time every 15 minutes when in doze mode. It depends on time you use. However when the system wakes up you have 10 seconds to do something as documentation says. – greywolf82 Sep 14 '15 at 06:52
  • @M66B The behavior you are seeing is now documented (https://developer.android.com/training/monitoring-device-state/doze-standby.html). With 'ignore optimizations' enabled you may hold a partial wakelock, when the device is in Doze idle and thus get some network access. It is still very important to note, that 'ignore optimizations' does NOT disable Doze mode for your app, this is extremely misleading. Apps behave strongly different in Doze mode, no matter if they are are excluded from optimizations or not. – sec_aw Oct 01 '15 at 10:35
  • 10
    Um, what's with the `if`? AFAIK, the determination of which of those two `Intent` actions you use is based on whether or not you want to request the `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission or not, not whether or not you are already on the whitelist. Am I missing something? – CommonsWare Oct 13 '15 at 23:25
  • 12
    Be careful with ignoring battery optimizations - my app just got suspended from Google Play without prior notice for requesting `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`, even though exempting the app from battery optimizations is crucial to my app's function, and I can't use GCM for technical reasons. – Elad Nava Oct 27 '15 at 15:20
  • 2
    How Photos app does backup in Doze mode on 6.0.1? It's not whitelisted. My app network access suspended in Doze, but Photos keeps uploading. – surlac Jun 03 '16 at 19:17
  • 2
    *Network access is disabled in doze mode, regardless if your application is ignoring battery optimizations.* that's incorrect *it is possible to let Android ignore battery optimization for your application, which effectively disables doze mode for your application.* that's also incorrect *setExactAndAllowWhileIdle (maximum one wakeup each 15 minutes)* this is 9 minutes now – Tim Aug 24 '16 at 08:55
  • Will apps get network switch (wifi-3g) notification while phone is in doze mode? – BTR Naidu Jan 25 '17 at 10:16
  • 1
    If already app is ignoring BatteryOptimization, what "intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATIO‌N_SETTINGS);" is for? Why do you need to open the setting page? – Bright Lee May 18 '17 at 20:35
  • 1
    This doesn't seem to be working with Android Wear 2.0. Anyone have any experience with this? – forresthopkinsa Jun 08 '17 at 23:08
  • @Bright Lee you should give the user an option to enabled battery optimizations again too, so see it as on and off. – M66B Jun 12 '17 at 16:01
  • @forresthopkinsa,Yeah it is not working in Android Wear 2.0.There in no option to manually Enable/Disable battery optimization . – kavie Jun 05 '18 at 09:39
0

According to the documentation, you can now have a foreground notification for your app which would defy the doze mode and your app should be able to access the network.

Anirudh Ganesh
  • 446
  • 4
  • 22
  • Please note that "if the device is idle for long periods of time, the system allows idle apps network access around **once a day**". That is not applicable in most cases. – Quicksilver Jun 03 '22 at 09:23