android.content.ActivityNotFoundException “android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS”

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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);             }         } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!