Seb Jachec
Seb Jachec

Reputation: 3061

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.

Upvotes: 3

Views: 710

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006809

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.

Upvotes: 2

Related Questions