Example usage for android.provider Settings ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

List of usage examples for android.provider Settings ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

Introduction

In this page you can find the example usage for android.provider Settings ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.

Prototype

String ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS

To view the source code for android.provider Settings ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.

Click Source Link

Document

Activity Action: Ask the user to allow an app to ignore battery optimizations (that is, put them on the whitelist of apps shown by #ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS ).

Usage

From source file:com.sentaroh.android.SMBSync2.ActivityMain.java

@SuppressLint("NewApi")
private void checkBatteryOptimization() {
    if (Build.VERSION.SDK_INT >= 23) {
        Intent intent = new Intent();
        String packageName = mContext.getPackageName();
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        if (pm.isIgnoringBatteryOptimizations(packageName))
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        else {/*from   ww  w  .ja v a 2s .  c o  m*/
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
        }
        startActivity(intent);
    }
}

From source file:be.blinkt.openvpn.activities.MainActivity.java

@TargetApi(Build.VERSION_CODES.M)
private void requestDozeDisable() {
    Intent intent = new Intent();
    String packageName = getPackageName();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
        intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {//from   w  w w. ja  v  a  2 s  .  c o  m
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
    }
    startActivity(intent);
}