【问题标题】:Open the native browser from an android app从 android 应用程序打开本机浏览器
【发布时间】:2012-05-07 03:40:35
【问题描述】:

我有一部安装了多个浏览器的安卓手机,我可能会也可能不会将浏览器设置为默认值。

所以,我的问题是..

  1. 如何从我的应用程序中强制仅在 NATIVE android 浏览器中打开链接?
  2. 有什么方法可以让我知道浏览器是否设置为默认值?

【问题讨论】:

    标签: android android-intent browser mobile-browser


    【解决方案1】:

    试试这样的。

    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();
          } 
    

    对于第二个问题,您可以使用PackageManager

    获取PackageManager的实例

    PackageManager packageManager = getPackageManager(); 
    

    并查询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 下面的代码回答了第一个问题。 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);对于问题的第二部分,有没有办法知道手机上是否设置了默认浏览器?
    • 我已将浏览器设置为默认值,但 resolveInfo.isDefault 似乎不起作用。
    【解决方案2】:

    从我的应用程序中,如何强制仅在 NATIVE android 中打开链接 浏览器?

    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();
     } 
    

    有没有办法知道浏览器是否设置为默认?

    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
    }
    

    【讨论】:

    • 以上代码未能识别默认浏览器。在我的设备上,我将 Opera 设置为默认浏览器,但是当我运行此代码时,我看不到 resolveInfo.isDefault 对于任何浏览器都是正确的!请帮忙。
    【解决方案3】:

    终于明白了。 resolveActivity 与 PackageManager 实例上的 MATCH_DEFAULT_ONLY 标志一起使用..

    【讨论】:

      【解决方案4】:

      调用原生浏览器需要做如下操作

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

      【讨论】:

        【解决方案5】:

        如果制造商的包名称不同,可能会发生 ActivityNotFoundException。 请参考这个答案,希望对您有所帮助。

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

        【讨论】:

          【解决方案6】:

          检查此代码:

          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");
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-11-06
            • 2017-01-29
            • 2012-03-21
            • 1970-01-01
            • 2019-01-17
            • 2013-08-17
            • 2011-03-20
            • 2019-01-01
            相关资源
            最近更新 更多