Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open a URL in Android's web browser from my application?

How to open an URL from code in the built-in web browser rather than within my application?

I tried this:

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(); } 

but I got an Exception:

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com 
like image 553
Arutha Avatar asked Feb 04 '10 17:02

Arutha


People also ask

How do I open a link in an Android app?

Every android app will have list of urls that it can open. So you have to go to that app settings and tell that it should open in browser for the urls and not in the app. To do that go to Settings -> Apps -> scroll down to the app that you don't want URLs to open in -> Tap on 'Open by Default' and select always Ask.


2 Answers

Try this:

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

That works fine for me.

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

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

I would also probably pre-populate your EditText that the user is typing a URL in with "http://".

like image 97
Mark B Avatar answered Oct 21 '22 23:10

Mark B


A common way to achieve this is with the next code:

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

that 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); 

the shortest! :

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"))); 
like image 34
Jorgesys Avatar answered Oct 21 '22 23:10

Jorgesys