Skip to content

Instantly share code, notes, and snippets.

@andreban
Created January 26, 2016 18:45
Show Gist options
  • Save andreban/1780525015f6449867a3 to your computer and use it in GitHub Desktop.
Save andreban/1780525015f6449867a3 to your computer and use it in GitHub Desktop.
public static Set<String> getNativeAppPackage(Context context, Uri uri) {
PackageManager pm = context.getPackageManager();
//Get all Apps that resolve a generic url
Intent browserActivityIntent
= new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
Set<String> genericResolvedList
= extractPackagenames(pm.queryIntentActivities(browserActivityIntent, 0));
//Get all apps that resolve the specific Url
Intent specializedActivityIntent = new Intent(Intent.ACTION_VIEW, uri);
Set<String> resolvedSpecializedList
= extractPackagenames(pm.queryIntentActivities(specializedActivityIntent, 0));
//Keep only the Urls that resolve the specific, but not the generic urls
resolvedSpecializedList.removeAll(genericResolvedList);
return resolvedSpecializedList;
}
public static Set<String> extractPackagenames(List<ResolveInfo> resolveInfos) {
Set<String> packageNameSet = new HashSet<>();
for (ResolveInfo ri: resolveInfos) {
packageNameSet.add(ri.activityInfo.packageName);
}
return packageNameSet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment