9

我正在尝试获取给定的默认/首选应用程序Intent。例如,当用户安装第二个 Web 浏览器,然后尝试打开一个 URL 时,他或她将看到如下对话框:

默认浏览器选择器

如果用户随后为此操作选择默认使用选项,则在按下 URL 时对话框将不再打开。

我正在开发一个应该知道这个默认首选应用程序/操作是什么的应用程序。我该怎么做呢?我目前正在使用下面的代码,但getPreferredPackage没有返回任何内容:

Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(i, 0);
IntentFilter ifilter = new IntentFilter(i.getAction());
if (i.getCategories() != null) {
    for(String category : i.getCategories()) {
        ifilter.addCategory(category);
    }
}
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(ifilter);
List<ComponentName> preferredActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(filters, preferredActivities, null);
for (ComponentName activity : preferredActivities) {
    for (ResolveInfo rinfo : list) {
        if (rinfo.activityInfo.applicationInfo.packageName.equals(activity.getPackageName())) {
            try {
                final PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0);
                Toast.makeText(context, pm.getApplicationLabel(pi.applicationInfo), Toast.LENGTH_SHORT).show();
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

我究竟做错了什么?这甚至是正确的方法吗?

4

3 回答 3

11

好吧,结果证明解决方案比我做的要简单得多(尽管文档记录很差)。以下代码是我的解决方案:

Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Toast.makeText(context, pm.getApplicationLabel(mInfo.activityInfo.applicationInfo), Toast.LENGTH_LONG).show();
于 2011-12-24T20:28:17.593 回答
6

下面的方法launchUrlInDefaultBrowser会启动一个 URL,而不向用户显示任何选择查询。首先,它会尝试找到用户的默认浏览器应用程序并使用它启动 URL。其次,如果没有默认应用程序,它会列出所有能够启动 URL 的活动并选择第一个。如果启动了活动,则该方法返回 true;否则为假。

boolean launchUrlInDefaultBrowser(Context context, String url) {
    final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
    browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    browserIntent.setData(Uri.parse(url));

    // 1: Try to find the default browser and launch the URL with it
    final ResolveInfo defaultResolution = context.getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
    if (defaultResolution != null) {
        final ActivityInfo activity = defaultResolution.activityInfo;
        if (!activity.name.equals("com.android.internal.app.ResolverActivity")) {
            browserIntent.setClassName(activity.applicationInfo.packageName, activity.name);
            context.startActivity(launchIntent);
            return true;
        }
    }
    // 2: Try to find anything that we can launch the URL with. Pick up the first one that can.
    final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
    if (!resolveInfoList.isEmpty()) {
        browserIntent.setClassName(resolveInfoList.get(0).activityInfo.packageName, resolveInfoList.get(0).activityInfo.name);
        context.startActivity(browserIntent);
        return true;
    }
    return false;
}

请注意,OEM 可能有自己的 ResolverActivity 实现。例如,华为有 com.huawei.android.internal.app.HwResolverActivity。

于 2014-05-23T03:38:59.677 回答
0

在 Kitkat AOSP 中,getPreferredPackages() 始终返回空列表。源代码如下

public List<PackageInfo> getPreferredPackages(int flags) {
    return new ArrayList<PackageInfo>();
}
于 2014-08-27T21:55:46.597 回答