android - 自定义 Intent 选择器 - 为什么在 Android 6 上它显示空单元格?

标签 android android-intent android-6.0-marshmallow share-intent android-intent-chooser

背景

我希望显示 native Intent 选择器,同时能够对其进行一些自定义。

为此,我找到了下一个 StackOverflow 线程:

How to customize share intent in Android?

问题

事情是,当我在 Android 5.x 及更低版本上使用建议的代码时,一切似乎都很好,但是当我在 Android 6.0.1 上使用它时(在 Nexus 5 和模拟器上测试,当有多个应用程序要共享时)内容),我得到空单元格,有时甚至是空应用程序名称,如下所示:

enter image description here

使用非自定义 Intent 选择器时不会出现此情况:

startActivity(Intent.createChooser(intent, "default chooser"));

enter image description here

代码

看到解决方案,我创建了下一个代码:

private void test(Intent shareIntent) {
    List<Intent> targetedShareIntents = new ArrayList<>();
    final List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            Intent targetedShareIntent = new Intent(shareIntent);
            targetedShareIntent.setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
            targetedShareIntents.add(targetedShareIntent);
        }
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
        startActivity(chooserIntent);
    }
}

private void prepareIntentToShare(Intent intent) {
    intent.setAction(android.content.Intent.ACTION_SEND);
    intent.setType("image/*");
    intent.putExtra(Intent.EXTRA_STREAM, mUri);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "title");
    intent.putExtra(android.content.Intent.EXTRA_TEXT, "body");
}

以及测试方法:

Intent intent = new Intent();
prepareIntentToShare(intent);
test(intent);

我尝试过的

我尝试过改变 Intent 中的各种内容,但没有任何运气。我还尝试找出 Intent 的顺序是什么(因为也许这很重要),但我没有找到。

最后,我决定将其发布到 Google,假设这是一个错误:

https://code.google.com/p/android/issues/detail?id=202693

问题

  1. 为什么会发生这种情况?我可以以某种方式修复它,同时仍然使用 native Intent 选择器吗?为什么只有Android 6及以上版本才会出现这种情况?

  2. 如何为每个项目输入正确的名称,例如,我看到“twitter”两次,但其他应用程序确实显示了正确的名称(例如二维码扫描仪的名称)?

  3. 是否可以保留如何订购应用程序的 native 行为,如使用显示 Intent 选择器的简单方式所示?也许可以按照应用程序的排序方式获取应用程序列表?

最佳答案

我花了一些时间阅读 ChooserActivityResolverActivity 并解决这些问题。

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);

    if (resolveInfos != null && !resolveInfos.isEmpty()) {
        List<Intent> targetIntents = new ArrayList<>();
        for (ResolveInfo resolveInfo : resolveInfos) {
            ActivityInfo activityInfo = resolveInfo.activityInfo;
            // remove activities which packageName contains 'ttt' for example
            if (activityInfo.packageName.contains("ttt")) {
                continue;
            }
            Intent targetIntent = new Intent(Intent.ACTION_SEND);
            targetIntent.setType("text/plain");
            targetIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.setting_share_app_subject));
            targetIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.setting_share_app_body));
            targetIntent.setPackage(activityInfo.packageName);
            targetIntent.setComponent(new ComponentName(activityInfo.packageName, activityInfo.name));

            // wrap with LabeledIntent to show correct name and icon

            LabeledIntent labeledIntent = new LabeledIntent(targetIntent, activityInfo.packageName, resolveInfo.labelRes, resolveInfo.icon);

            // add filtered intent to a list
            targetIntents.add(labeledIntent);
        }
        Intent chooserIntent;
        // deal with M list seperate problem
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // create chooser with empty intent in M could fix the empty cells problem
            chooserIntent = Intent.createChooser(new Intent(), context.getString(R.string.setting_share_app_title));
        } else {
            // create chooser with one target intent below M
            chooserIntent = Intent.createChooser(targetIntents.remove(0), context.getString(R.string.setting_share_app_title));
        }
        if (chooserIntent == null) {
            return;
        }
        // add initial intents
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[targetIntents.size()]));
        try {
            context.startActivity(chooserIntent);
        } catch (ActivityNotFoundException e) {
            Logger.e(TAG, e, e);
        }
    }

编辑:在 Android Q (10 - API 29) 上这似乎已损坏,并且最多只会显示 2 个项目,而不是全部。又问这个here .

编辑:制作了一个选择从共享中排除哪些项目的示例,here ,这应该适用于所有 Android 版本。

关于android - 自定义 Intent 选择器 - 为什么在 Android 6 上它显示空单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35842612/

相关文章:

android - Cordova 3.0 插件,找不到类

android - 如何让我的分享 Intent 支持 whatsapp 和 google+

android - Android 6.0无法启动服务

android - 由于 OTA 更新到 Marshmallow,我基于陀螺仪的应用程序不再工作

android - Logcat 不会显示 Log.d 消息

android - 避免使用AGPS或网络GPS以避免扣钱 Android

javascript - 在 Android 的 WebView 中通过 javascript 捕获页面重定向

android - 从 Google Play 商店安装应用程序后如何打开特定 Activity 以获得深层链接

java - 是否可以使用 createChooser 但尚未实际启动 Intent ?

安卓 6 : App uses more memory each time it's started