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

Asked 1 years ago, Updated 1 years ago, 110 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.