Reputation: 22183
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?
Upvotes: 12
Views: 16999
Reputation: 1204
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.
Upvotes: 10
Reputation: 476
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.
Upvotes: 0