Marshmallow APIs are very different from previous android OS. When screen is off, devices are in doze mode and unable to sync network. So for doing background operations with network we have to prevent from doze mode.
Asked
Active
Viewed 8,719 times
8
-
3What you want is not possible, except via a custom ROM or possibly on rooted devices. The user can manage the battery optimization whitelist directly from the Settings app. – CommonsWare Jun 01 '16 at 12:25
-
I have seen some apps in playstore are controlling doze mode. How does they do? – Krishna Jun 03 '16 at 12:14
-
Name any such app, that does not require root. – CommonsWare Jun 03 '16 at 12:14
-
doze app :- https://play.google.com/store/apps/details?id=com.yirgalab.dzzz&hl=en – Krishna Jun 09 '16 at 09:04
-
1The *app* is named "Doze". It would appear to maintain a whitelist of apps that *it* does not affect. – CommonsWare Jun 09 '16 at 10:25
-
how to add our app in whitelist ? Can we do it programmatically ? – Krishna Jun 11 '16 at 05:39
-
1You cannot add an app to the whitelist programmatically. You can ask the user to add your app to the whitelist, but [doing so may get you banned from the Play Store](https://commonsware.com/blog/2015/11/11/google-anti-trust-issues.html) (if that is where you planned to distribute your app). – CommonsWare Jun 11 '16 at 11:07
-
1I think this question can be useful. The app I am developing is commercial use and not distribute to Google Play.... And now the customers complain about no network activities while screen off... – Yeung Mar 14 '19 at 05:35
-
yeah, people forget that not all apps go through the PlayStore – Alberto M Mar 17 '21 at 15:58
1 Answers
12
Add below permission to manifest.xml
<uses-permission
android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Call below method
public void turnOffDozeMode(Context context){ //you can use with or without passing context
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName)) // if you want to desable doze mode for this package
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else { // if you want to enable doze mode
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);
}
}
Or you can use below scenario also...
Whitelisting an Android application programmatically can be done as follows:
boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
if(!isIgnoringBatteryOptimizations){
Intent intent = new Intent();
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, MY_IGNORE_OPTIMIZATION_REQUEST);
}
The result of starting the activity above can be verfied by the following code:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_IGNORE_OPTIMIZATION_REQUEST) {
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
boolean isIgnoringBatteryOptimizations = pm.isIgnoringBatteryOptimizations(getPackageName());
if(isIgnoringBatteryOptimizations){
// Ignoring battery optimization
}else{
// Not ignoring battery optimization
}
}
}

SWAPDROiD
- 357
- 3
- 15