从安卓应用程序中打开本机浏览器

8

我有一个安卓手机,上面装有多个浏览器,我可能设置了一个默认浏览器,也可能没有。

因此,我的问题是...

  1. 从我的应用程序中,如何强制在 本地 安卓浏览器中打开链接?
  2. 是否有一种方法可以知道是否设置了默认浏览器?
6个回答

7
从我的应用程序中,如何强制在本机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
}

1
以上代码未能识别默认浏览器。在我的设备上,我将Opera设置为默认浏览器,但当我运行此代码时,我发现resolveInfo.isDefault从未对任何浏览器返回true!请帮忙。 - defactodeity

5
您需要按照以下步骤调用本机浏览器。
  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));

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);对于第二个问题,是否有办法知道手机上是否设置了默认浏览器? - defactodeity
我已将浏览器设置为默认,但 resolveInfo.isDefault 似乎无法正常工作。 - defactodeity

1

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


1

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接