Ramesh Yogu
Ramesh Yogu

Reputation: 49

How solve a Fatal Exception in android.content.ActivityNotFoundException Error

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 error Showed in OPPO F3 Mobiles, 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.

Upvotes: 2

Views: 726

Answers (1)

shmakova
shmakova

Reputation: 6426

On some devices this activity may not exist, because of custom firmware.

You can check your intent by using this method:

private static boolean isIntentAvailable(@NonNull Context context, @NonNull Intent intent) {
    return context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}

And when you do startActivity do this:

Intent intent = new Intent("android.settings.IGNORE_BATTERY_OPTIMIZATION_SETTINGS");
if (isIntentAvailable(this, intent)) {
     startActivity(intent);
} else {
     // Do something else
}

Or catch your exception. Or find these custom activities on problem devices and call them.

Upvotes: 1

Related Questions