Intent to open link in browser does not work

Asked

Viewed 3,646 times

3

How do I get my button to send the user to a link?

String url = "LINK";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

I tried this way but it didn’t work

  • 1

    the shape is right but in place of the link is the same link... it = new Intent(Intent.ACTION_VIEW); it.setData(Uri.parse("http://www.whatsapp.com")); or String url = "http://www.whatsapp.com" and then it.setData(url Uri.parse(url ));

  • I know, I took the link , thanks friend

1 answer

12


Your code is correct.

What should be happening is that you are using a URL without the protocol, like "google.com".

The URL should include the "http://" or "https protocol://".

String url = "http://google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

You can create a method that ensures the link has the protocol added:

public static browseTo(String url){

    if (!url.startsWith("http://") && !url.startsWith("https://")){
        url = "http://" + url;
    }
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.