public class Battery {
public static void addWhite(Activity activity){
PowerManager packageManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
//应用是否在 白名单中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!packageManager.isIgnoringBatteryOptimizations(activity.getPackageName())){
//方法1、启动一个 ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent
// Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
// activity.startActivity(intent);
//方法2、触发系统对话框
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+activity.getPackageName()));
activity.startActivity(intent);
}
}
}
}