Open the native browser from an android app

血红的双手。 提交于 2019-11-30 07:15:11

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
}

You have to make the following for calling the native browser

  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));

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

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

Bastet

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!