Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 938
defactodeity Avatar asked Apr 26 '12 11:04

defactodeity


People also ask

What is the native browser on Android?

And most of us typically put up with our Android's default web browser, which is usually Google Chrome, or Samsung Internet if you have a Samsung device. But with how easy it is to switch your phone's default internet browser, you shouldn't have to put up with a less-than-perfect browser.

How do I set an app to open links as default?

Change Link Opening Settings To change how links open in Android, return to the Default apps page you visited earlier. Here, tap Opening links to review these settings. At the top, you can toggle the Instant apps feature, which allows you to use some apps without actually installing them.


3 Answers

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
}
like image 121
grassyburrito Avatar answered Oct 03 '22 12:10

grassyburrito


You have to make the following for calling the native browser

  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));
like image 31
Pramod J George Avatar answered Oct 03 '22 13:10

Pramod J George


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
         }
    }
like image 20
N-JOY Avatar answered Oct 03 '22 13:10

N-JOY