Sunday, June 3, 2012

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 :




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



startActivity(myIntent);




but I got an Exception : "No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com"


Source: Tips4all

2 comments:

  1. 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://".

    ReplyDelete
  2. In 2.3, I had better luck with

    final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
    activity.startActivity(intent);


    The difference being the use of Intent.ACTION_VIEW rather than the String "android.intent.action.VIEW"

    ReplyDelete