Skip to content Skip to sidebar Skip to footer

Setting Default Browser For Activity Programmatically

I'm developing an android app and among other functionality I need to open some urls in external web browser. Can I programmatically set a default application for that, so the user

Solution 1:

Yes, for this you can force your application to always open native android browser only. For this you have to identify the launching Activity of Browser application, something like this:

Intentintent=newIntent();
intent.setComponent(newComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity"));
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
Uriuri= Uri.parse(url);
intent.setData(uri);
try
{
    startActivity(intent);
}
catch (Exception e)
{
   e.printStackTrace();
}

Solution 2:

You can use .setPackage for the intent: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String) . Call it with the browser's package name (defined in its manifest, package attribute).

I'm using something similar for firing up the Google+ application for sharing a string:

Intent shareIntent = ShareCompat.IntentBuilder.from(getActivity())
           .setText("Dummy string to share")
           .setType("text/plain")
           .getIntent()
           .setPackage("com.google.android.apps.plus");

        startActivity(shareIntent);

In my example, "com.google.android.apps.plus" is the package name for the Google+ application.

Solution 3:

Sharedpreference for Browser class is "MODE_private" , so we can't access the home_page changing steps directly programatically,

iff we want to do, we should do through Browser.java opensource code and we should get some idea from there itself.

Post a Comment for "Setting Default Browser For Activity Programmatically"