I have an Android app that does this:
PackageManager pm = getApplicationContext().getPackageManager();
Intent browserIntent = new Intent();
browserIntent.setAction(Intent.ACTION_VIEW);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
browserIntent.setData(Uri.parse("http://www.google.com"));
ResolveInfo resolveInfo = pm.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
try {
String browserType = nutent.activityInfo.packageName;
} catch (NullPointerException npe) {
npe.printStackTrace();
}
I have tested this on 4 different handsets and many different emulator configurations. All of them work fine. But I'm getting crash reports from deployed applications in the wild throwing an NPE since resolveInfo is sometimes null.
The immediate fix is to catch the NPE and deal with it before it crashes my app (as done above). But I've tried for days to reproduce this myself and cannot. On an emulator with a single default browser installed, it resolves to com.android.browser.BrowserActivity:
ResolveInfo{411ef228 com.android.browser.BrowserActivity p=0 o=0 m=0x208000}
Likewise, on a handset with multiple browsers installed, this resolves somewhat differently...to com.android.internal.app.ResolverActivity:
ResolveInfo{415c13b8 com.android.internal.app.ResolverActivity p=0 o=0 m=0x0}
The resolveActivity() docs here state:
Returns a ResolveInfo containing the final activity intent that was determined to be the best action. Returns null if no matching activity was found. If multiple matching activities are found and there is no default set, returns a ResolveInfo containing something else, such as the activity resolver.
So I have covered the first scenario, that of an activity intent determined to be the best action. Also the last scenario, that of multiple matching activities with no default action, returning the ResolverActivity.
I would really like to reproduce the scenario of resolveActivity() giving me a null when requesting the activity of a URL. Does anyone have any ideas how this can be done without, say, rooting the device?