Created
July 1, 2020 16:05
-
-
Save hborders/f7da02c55a9132c623f7bd95934e6b13 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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