问题
I'm trying to guide my users to the battery optimizations activity and it seems to be working for most except for some Samsung phones with Android 6 where I get:
Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS }
This is what I am using to launch it:
Intent intent = new Intent("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS");
startActivity(intent);
Any idea what I should be launching on those phones?
Thanks.
回答1:
Use below code to check whether your app already ignored for battery optimization and if yes then show popup to enable the same.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(getPackageName())) {
Intent batterySaverIntent=new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:"+getPackageName()));
startActivity(batterySaverIntent);
}
}
来源:https://stackoverflow.com/questions/42909119/android-content-activitynotfoundexception-android-settings-ignore-battery-optim