Showing posts with label Android Stuff. Show all posts
Showing posts with label Android Stuff. Show all posts

Friday, November 19, 2010

Opening browser through intent Android 2.1

If you're trying to open the Android browser through intent like this:

siteURL = Uri.parse("google.com");
Intent browserIntent = new Intent(Intent.ACTION_VIEW, siteURL);
mContext.startActivity(browserIntent);

It's most definitely going to crash. When starting browser through intent, you've got to set the scheme for the url (such as http). If your url does not contain a scheme, like the example above, make use of the Uri class to build a Uri using the scheme before starting the browser, like this (a very crude code snippet, feel free to add any refinements in your comments):

siteURL = Uri.parse("google.com");
Uri.Builder builder = new Uri.Builder();
builder.scheme("http");
ssP = finalURL.getSchemeSpecificPart();
if(!ssP.startsWith("//"))
    ssP = "//" + ssP;
Uri.encode(ssP, "/");
builder.encodedAuthority(finalURL.getSchemeSpecificPart());