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

Phi*_*hil 9 android android-package-managers

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

默认浏览器选择器

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

我正在开发一个应该知道这个默认首选 app/action的应用程序.我该怎么做呢?我目前正在使用下面的代码,但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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Phi*_*hil 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();
Run Code Online (Sandbox Code Playgroud)

  • 如果给定意图没有默认应用程序怎么办?你怎么知道这件事? (4认同)
  • 如果没有此Intent的默认活动,这里我得到的是这样的.ResolveInfo {40df0c50 com.android.internal.app.ResolverActivity p = 0 o = 0 m = 0x0} (2认同)

Juu*_*nen 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;
}
Run Code Online (Sandbox Code Playgroud)

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