Android跳转到省电优化权限界面
//省电优化
Intentintent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = ComponentName.unflattenFromString("com.android.settings/.Settings$HighPowerApplicationsActivity");
intent.setComponent(cn);
LCApplication.context.startActivity(intent);
需要注意,这个有些手机没有,例如oppo(Android 4.0)没有,三星(版本未知)也没有,目前我发现支持的手机有小米5X(Android 7.0的系统),华为(没注意到是啥版本,Android系统为6.0以上)。其他的没测过。
第二种方法
/**
* 忽略电池优化
*/
public void ignoreBatteryOptimization(Activity activity) {
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean hasIgnored = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
hasIgnored = powerManager.isIgnoringBatteryOptimizations(activity.getPackageName());
// 判断当前APP是否有加入电池优化的白名单,如果没有,弹出加入电池优化的白名单的设置对话框。
if(!hasIgnored) {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:"+activity.getPackageName()));
startActivity(intent);
}
}
}
第二种方法要直白点,第一种是用户自己手动设置,第二种是自已跳出来让用户授权,但是只支持23以上的手机设备
此外还需要在AndroidManifest.xml里加入权限<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"></uses-permission>