Sending an Intent to browser to open specific URL

2018-12-31 12:44发布

This question already has an answer here:

I'm just wondering how to fire up an Intent to the phone's browser to open an specific URL and display it.

Can someone please give me a hint?

10条回答
爱死公子算了
2楼-- · 2018-12-31 13:27
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
查看更多
不流泪的眼
3楼-- · 2018-12-31 13:28

In some cases URL may start with "www". In this case you will get an exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent

The URL must always start with "http://" or "https://" so I use this snipped of code:

if (!url.startsWith("https://") && !url.startsWith("http://")){
    url = "http://" + url;
}
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(openUrlIntent);
查看更多
伤终究还是伤i
4楼-- · 2018-12-31 13:28

Sending an Intent to Browser to open specific URL:

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

could be changed to a short code version ...

Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));      
startActivity(intent); 

or

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

or even more short!

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

More info about Intent

=)

查看更多
与君花间醉酒
5楼-- · 2018-12-31 13:29

The shortest version.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
查看更多
登录 后发表回答