How to check if startActivity is Allowed in background (Android)?

31 Views Asked by At

When notification is received Activity can not be launched due to background restrictions in android

How to check i am allowed to call startActivity through code without try catch block https://developer.android.com/guide/components/activities/background-starts

1

There are 1 best solutions below

0
Hezy Ziv On

Use PackageManager

Intent intent = new Intent(this, YourTargetActivity.class);

// Check if there is at least one activity that can handle the intent packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);

if (!activities.isEmpty()) {
    // There's at least one activity that can handle this intent, so it's safe to start it
    startActivity(intent);
} else {
    // No activity can handle the intent, handle this case as needed (e.g., show an error message)
}