Skip to content

Instantly share code, notes, and snippets.

@Ray33
Created February 26, 2017 13:01
Show Gist options
  • Save Ray33/7ce134b68159a9ad6a7b773b9a4da55f to your computer and use it in GitHub Desktop.
Save Ray33/7ce134b68159a9ad6a7b773b9a4da55f to your computer and use it in GitHub Desktop.
Get browser package
public static String getBrowserPackage(Intent intent, Context context){
PackageManager pm = context.getPackageManager();
List<ResolveInfo> appsList = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
String tmpPkg = "", pkg="";
for (ResolveInfo app : appsList){
tmpPkg = app.activityInfo.packageName.toLowerCase();
if ("com.android.chrome".equals(tmpPkg)){
return tmpPkg;
}
if (tmpPkg.contains("browse") || tmpPkg.contains("chrome") || tmpPkg.contains("com.UCMobile.intl") || tmpPkg.contains("org.mozilla.firefox") || tmpPkg.contains("com.opera.mini.native")){
pkg = tmpPkg;
}
}
return pkg;
}
@Ray33
Copy link
Author

Ray33 commented Feb 26, 2017

Usage:
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(SOME_URL));
final String browserPackage = ActivityUtils.getBrowserPackage(intent, context);
if (!browserPackage.isEmpty()) {
intent.setPackage(browserPackage);
} else {
intent.setPackage(null);
}
startActivity(intent);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment