Search code examples
androidandroid-intentbrowsermobile-browser

Open the native browser from an android app


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?

Solution

  • 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
    }