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
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.
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://".
String url = "http://www.stackoverflow.com"; Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i);
Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com")); startActivity(intent);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")); startActivity(intent);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com")));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With