8

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

所以,我的问题是..

  1. 从我的应用程序中,如何强制仅在NATIVE android 浏览器中打开链接?
  2. 有没有办法知道浏览器是否设置为默认?
4

6 回答 6

7

从我的应用程序中,如何强制仅在 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
}
于 2012-04-26T12:54:59.113 回答
5

您必须执行以下操作才能调用本机浏览器

  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));
于 2012-10-22T05:26:44.920 回答
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
         }
    }
于 2012-04-26T12:15:44.470 回答
1

终于想通了。resolveActivity 与 PackageManager 实例上的 MATCH_DEFAULT_ONLY 标志一起使用。

于 2012-05-03T07:10:18.547 回答
1

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

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

于 2013-02-06T07:30:10.610 回答
0

检查此代码:

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");
    }
}
于 2020-01-01T23:06:38.373 回答