how to turn off doze mode for specific apps in marshmallow devices programmatically
Asked Answered
A

1

9

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.

Armor answered 1/6, 2016 at 12:9 Comment(9)
What 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.Thibodeau
I have seen some apps in playstore are controlling doze mode. How does they do?Armor
Name any such app, that does not require root.Thibodeau
doze app :- play.google.com/store/apps/…Armor
The app is named "Doze". It would appear to maintain a whitelist of apps that it does not affect.Thibodeau
how to add our app in whitelist ? Can we do it programmatically ?Armor
You 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 (if that is where you planned to distribute your app).Thibodeau
I 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...Cavorelievo
yeah, people forget that not all apps go through the PlayStoreAnubis
P
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
        }
    }
}
Polyphony answered 27/8, 2018 at 6:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.