Yury Pogrebnyak
Yury Pogrebnyak

Reputation: 4173

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 won't be able to choose from the list of available browsers? I mean, I want to set default browser only for my app but not for the whole operating system.

Upvotes: 1

Views: 8976

Answers (3)

Naveen AH
Naveen AH

Reputation: 33

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.

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

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:

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

Upvotes: 3

Buffalo
Buffalo

Reputation: 4042

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.

Upvotes: 2

Related Questions