I'm testing one application in one Android 6 where I need to pass the DOZE, for that I want to request to the users accept the igore of battery optimization.
As in project AntiDoze, but when I run in a virtual device I do not have any problem, but when I pass to the Samsung I get:
FATAL EXCEPTION: main
Process: com.commonsware.android.antidoze, PID: 21135
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.commonsware.android.antidoze/com.commonsware.android.antidoze.EventDemoActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:com.commonsware.android.antidoze }
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
Any idea how to pass this problem?
The code important from the link is:
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
String pkg=getPackageName();
PowerManager pm=getSystemService(PowerManager.class);
if (!pm.isIgnoringBatteryOptimizations(pkg)) {
Intent i=
new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
.setData(Uri.parse("package:"+pkg));
startActivity(i);
}
}
In the manifest I have
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
Catch the ActivityNotFoundException
and display your own UI to the user, asking them to go to the Settings app and add your app to the battery optimization whitelist. While the docs do not say this specifically for this Intent
action, most of the Settings.ACTION_*
values mention "In some cases, a matching Activity may not exist, so ensure you safeguard against this".