8

I have an android phone with multiple browsers installed and I might or might not set a browser to default.

So, my question is..

  1. From my App, How do I force open a link only in the NATIVE android browser?
  2. Is there a way I can know if there is a browser set to default or not?
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
defactodeity
  • 714
  • 1
  • 8
  • 21

6 Answers6

7

From my App, How do I force open a link only in the NATIVE android browser?

Intent intent = new Intent();
ComponentName comp = new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity");
intent.setComponent(comp);
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();
 } 

Is there a way I can know if there is a browser set to default or not?

PackageManager packageManager = getPackageManager();

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("URL"));

List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);

if (list.size() > 0) {
    for (ResolveInfo resolveInfo : list) {
       resolveInfo.isDefault();// will let u know if the app is set to default
    }

} else {
    //No apps available
}
grassyburrito
  • 1,213
  • 22
  • 32
  • 1
    The above code has failed to identify the default browser. On my device, I set Opera as default browser but when I run this code, I don't see `resolveInfo.isDefault` is ever true for any browser! Please help. – defactodeity May 02 '12 at 10:27
5

You have to make the following for calling the native browser

  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));
Pramod J George
  • 1,723
  • 12
  • 24
1

try something like this.

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");
      ContentURI uri = new ContentURI(url);
      i.setData(uri);
      startActivityForResult(i, 2);
      } catch (URISyntaxException e) {
                       e.printStackTrace();
      } 

for your second question you can use PackageManager.

get instance of PackageManager

PackageManager packageManager = getPackageManager(); 

and query it for specific action, data and category of Intent.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("URL"));

List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);

    for (ResolveInfo resolveInfo : list) {

       if(resolveInfo.isDefault())
        {
        //default browser
         }
    }
N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • Thanks a lot @N-JOY The following code answered the first question. `Intent ii = new Intent(); ComponentName comp = new ComponentName("com.android.browser","com.android.browser.BrowserActivity"); ii.setComponent(comp); ii.setAction("android.intent.action.VIEW"); ii.addCategory("android.intent.category.BROWSABLE"); ii.setData(Uri.parse("http://www.google.com")); startActivity(ii);` For the second part of the question, **Is there a way to know if there's a default browser set on the phone?** – defactodeity Apr 26 '12 at 12:51
  • I have set a browser to default but `resolveInfo.isDefault` doesn't seem to work. – defactodeity May 02 '12 at 11:50
1

Finally figured it out. resolveActivity works with MATCH_DEFAULT_ONLY flag on PackageManager instance..

aasha
  • 446
  • 4
  • 11
1

it might occur ActivityNotFoundException while the package name is different by manufacturer. Please refer to this answer, wish it would help.

https://stackoverflow.com/a/14723703/1083128

Community
  • 1
  • 1
Mia
  • 1,226
  • 1
  • 19
  • 29
0

Check this code:

private final static List<ComponentName> browserComponents  = new ArrayList<ComponentName>() {{
    add(new ComponentName("com.google.android.browser", "com.google.android.browser.BrowserActivity"));
    add(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
    add(new ComponentName("com.android.chrome", "com.google.android.apps.chrome.IntentDispatcher"));
}};

public static void openInNativeBrowser(Context context, String url) {
    if (context == null || TextUtils.isEmpty(url)) {
        return;
    }

    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_BROWSABLE);
    intent.setData(Uri.parse(url));

    for (ComponentName cn : browserComponents) {
        intent.setComponent(cn);
        ActivityInfo ai = intent.resolveActivityInfo(pm, 0);
        if (ai != null) {
            aLog.w(TAG, "browser:  " + ai);
            context.startActivity(intent);
            return;
        } else {
            aLog.w(TAG, "can't resolve activity for " + intent);
        }
    }

    // no native browser
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
    if (list.size() > 0) {
        for (ResolveInfo ri : list) {
            aLog.w(TAG, ri + " : " + ri.isDefault);
        }
        context.startActivity(intent);
    } else {
        aLog.w(TAG, "no browser apps");
    }
}
B-GangsteR
  • 2,534
  • 22
  • 34