'Android 12 open link in Chrome intent

Since Android 12, links can only be opened with one "approved" app. As of right now I have my app's supported urls defined in AndroidManifest.xml as intent-filter. Also I have a button in my app that creates an intent to open a link in browser.

With Android 12 limitations though, if I have the url tied to my app, clicking the open in browser button does re-open my app, which is quite an unwanted behavior.

Is there a way to force a url open in chrome (or other browser)? I checked the android developer documentation but have not found anything about it.

Thank you



Solution 1:[1]

You can specify the default browser explicitly as the package to handle your intent:

val defaultBrowserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))

// This would be Chrome if it's the selected default browser
val defaultBrowserPackageName = packageManager.resolveActivity(
  defaultBrowserIntent, 
  PackageManager.MATCH_DEFAULT_ONLY
)
  ?.activityInfo
  ?.packageName

val yourIntent = Intent(context, ...).apply {
  package = defaultBrowserPackageName
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 arkon