Skip to content

Instantly share code, notes, and snippets.

@hborders
Created July 1, 2020 16:05

Revisions

  1. hborders created this gist Jul 1, 2020.
    15 changes: 15 additions & 0 deletions LaunchBrowserActivity.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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
    }
    }
    }
    }