Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Intent calling specific Component is callable?

I have an intent designed to open the Data Usage Summary view of the system settings app (undocumented; from this Stack Overflow answer):

Intent openIntent = new Intent(Intent.ACTION_MAIN);
openIntent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
openIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(openIntent);

Is it possible to check whether this component exists and whether the intent will execute successfully?

A similar question gave answers that do not work for this intent in the Android (5.0) Emulator (causing the Settings app to crash several times over – see stacktrace). The below code answers return true (i.e. success), even though my above code will crash the Settings app. My activity intent has so far only crashed on the Emulator presumable due to there being no data plan set(?)

private boolean isCallable(Intent intent) {
    List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 
        PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

From this answer..

and

boolean activityExists = intent.resolveActivityInfo(getPackageManager(), 0) != null;

From this one..

Thanks.

like image 951
Seb Jachec Avatar asked Mar 03 '26 02:03

Seb Jachec


1 Answers

The reason why the code snippets you tried are saying that the component is available is because the component is available. The component happens to crash when you try starting it. Whether this is due to an emulator bug, an Android bug, or the fact that you happen to be starting an activity that isn't documented to be started by third-party apps, I can't say.

Is it possible to check whether this component exists

Use the code snippets from your question. In this case, the component exists.

Is it possible to check whether... the intent will execute successfully?

Not in general. Third-party applications are written by third parties. Not only might they have bugs/limitations, but you have no means of determining whether they do from your app.

like image 134
CommonsWare Avatar answered Mar 05 '26 16:03

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!