Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Network access in doze mode

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?

like image 415
greywolf82 Avatar asked Aug 31 '15 17:08

greywolf82


1 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.

like image 81
M66B Avatar answered Oct 31 '22 18:10

M66B