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?
2 Answers
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);
if (pm.isIgnoringBatteryOptimizations(packageName))
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.
-
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: code.google.com/p/android-developer-preview/issues/…– sec_awSep 8, 2015 at 11:16
-
2Note 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.– M66BSep 9, 2015 at 12:29
-
3setExactAndAllowWhileIdle 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. Sep 14, 2015 at 6:52
-
8Um, what's with the
if
? AFAIK, the determination of which of those twoIntent
actions you use is based on whether or not you want to request theREQUEST_IGNORE_BATTERY_OPTIMIZATIONS
permission or not, not whether or not you are already on the whitelist. Am I missing something? Oct 13, 2015 at 23:25 -
11Be 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. Oct 27, 2015 at 15:20
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.