Skip to content

Instantly share code, notes, and snippets.

@hborders
Created July 1, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hborders/f7da02c55a9132c623f7bd95934e6b13 to your computer and use it in GitHub Desktop.
Save hborders/f7da02c55a9132c623f7bd95934e6b13 to your computer and use it in GitHub Desktop.
fun launchBrowser(context: Context, uri: Uri?) {
val browserIntent = Intent(Intent.ACTION_VIEW).apply { data = uri }
val browseActivities = context.packageManager.queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (info in browseActivities) {
if (info.activityInfo.packageName != context.packageName) {
browserIntent.setPackage(info.activityInfo.packageName)
try {
context.startActivity(browserIntent)
break
} catch (e: ActivityNotFoundException) {
// No-op, Activity unable to handle intent, try another
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment