简体   繁体   中英

Open a URL in Android's web browser from application

How can I open a url in the android web browser from my application on clicking a button.

I tried this :

Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));

startActivity(myIntent);

but I am getting exception:

but I got an Exception :

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

You are doing it fundamentally correct, you just need to include the full url.

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

Basically, the http is the protocol. It is the computer's way of trying to figure out how you want to open it. You might also be interested in https , if you want a secure connection over SSL.

Almost seems like this would be useful to read... from the page:

Uri uri = Uri.parse("http://androidbook.blogspot.com");
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uri);
startActivity(launchBrowser);

There is a second way, involving the WebView , but that seems like it's not your question. Hope this helped.

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

Follow this For More Detail

Try this:

String strURL = "http://www.google.com";
        Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(strURL));

        startActivity(myIntent);

As for the missing "http://" I'd just do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM