更新 BatteryOptimizationUtil.java

dev
wangsiyuan 2023-12-08 14:32:08 +08:00
parent 4cd24ebe02
commit 08403b9109
1 changed files with 7 additions and 4 deletions

View File

@ -32,19 +32,22 @@ public class BatteryOptimizationUtil {
*
*/
public static void requestIgnoreBatteryOptimization(Context context) {
if (isAppIgnoringBatteryOptimizations(context)) {
Timber.d("已在忽略电池优化白名单");
return;
}
Timber.d("请求电池优化白名单");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
Intent intent = new Intent();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 添加FLAG_ACTIVITY_NEW_TASK标志
context.startActivity(intent);
}
}
}
}