在Android上获取首选/默认应用

时间:2011-12-24 19:18:48

标签: android android-package-managers

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

default browser selector

如果用户随后选择默认情况下使用此操作选项,则在按下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();
        }
    }
}

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

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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。

答案 2 :(得分:0)

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

public List<PackageInfo> getPreferredPackages(int flags) {
    return new ArrayList<PackageInfo>();
}