I want to open the URL with my Android web browser on my app

Asked 2 years ago, Updated 2 years ago, 138 views

    try {
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
        startActivity(myIntent);
    } } catch (ActivityNotFoundException e) {
        Toast.makeText(this, "No application can handle this request."
            + + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }

I tried to open a URL.

    No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

There was an error.

android url android-intent android-browser

2022-09-22 13:37

1 Answers

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
    startActivity(browserIntent);

Do it like this. When url is used as a variable, prepare for when http:// is not attached

    if (!url.startsWith("http://") && !url.startsWith("https://"))
       url = "http://" + url;

It's good to do it like this.


2022-09-22 13:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.