3

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.

Community
  • 1
  • 1
Seb Jachec
  • 3,003
  • 2
  • 30
  • 56
  • "causing the Settings app to crash several times over" -- please edit your question and provide the complete stack trace. The answers that you linked to are correct, and so we need the stack trace to advise you of where you are going wrong. – CommonsWare Dec 26 '15 at 17:09
  • @CommonsWare I've added it, but the issue here shouldn't be with anything peculiar I'm doing. This is a problem that has only occurred within the Emulator due to the "Bandwidth module" being disabled, and shouldn't happen 'in the wild'. I was just wondering if there's any way to check/catch this just in case though. **Edit**: Just fixed the pastebin formatting. – Seb Jachec Dec 26 '15 at 17:20

1 Answers1

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, I guess that's a pretty straightforward answer – makes sense. My intent accesses something undocumented, so I hoped there was some better way to know if this would cause a problem (crashing Settings) on devices in future if Google close up access/remove it etc. – Seb Jachec Dec 26 '15 at 17:46
  • @SebJachec: "in future if Google close up access/remove it etc." -- if they remove it outright, the code in your question will detect that. If they make it be not exported (`android:exported="false"`) or require some permission for it, you should get a `SecurityException` in your app... hopefully thrown by the `startActivity()` call (I have not tried this recently) so you can catch it. But, in this case, it's no different than if they had a typo in a `TextView` or had a divide-by-zero error. Bugs that are in the component crash the component; bugs in *calling* the component crash the caller. – CommonsWare Dec 26 '15 at 17:56
  • I see. Thanks for explaining – Seb Jachec Dec 26 '15 at 17:58