1

My app consits of a web view and has the option to register as handler for links to https://example.com/news.

I have an 'Open in Browser' button that should open the current url in an external browser.

If my app is set as default handler for links to https://example.com/news, then the 'Open in Browser' button does not offer the option to open the url with a browser. Instead my app automaticalls opens the url.

I have seen that it's possible force an app to open urls in chorme, however I want a more general solution that works even if chorme is disabled/not installed. My question is then, how to open the url using any or the default browser?

The code for the 'Open in Browser' button:

if (url != null) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
Alex
  • 43
  • 2
  • 7

1 Answers1

1

Your implementation looks good to me and you can use it whether chrome is installed or not but you need to check if it is resolvable or not:

if (url != null) {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (sendIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

If you want to show list of browsers to user to choose from you can use:

Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
     startActivity(chooser);
}

If your targetSdk is 30 or above take a look at this document too: https://developer.android.com/training/package-visibility/use-cases

AliSh
  • 10,085
  • 5
  • 44
  • 76