2010/07/05

Open an external browser on Android

Ok, so I wanted to open an external browser from my Android application. Crazy, right? Anyways, after some searching and trying to read almost empty Android SDK docs on the subject, I came across this golden turd:


        try{  
            Intent i = new Intent();
            ComponentName comp = new ComponentName(
                             "com.google.android.browser",
                                    "com.google.android.browser.BrowserActivity");
            i.setComponent(comp);
            i.setAction("android.intent.action.VIEW");
            i.addCategory("android.intent.category.BROWSABLE");
            Uri.Builder z = new Uri.Builder();          
            i.setData(z.build());
            getContext().startActivity(i);
        } catch (URISyntaxException e) {
                e.printStackTrace();
        }



At this point I lost all faith in humanity and all that's sacred.
Come on people, there must be a better way!

And here I come to save the day :) The code is actually very short and simple:


getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

No comments:

Post a Comment