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

时间:2016-03-07 11:32:36

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

背景

我希望展示本机意图选择器,同时能够自定义它。

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

How to customize share intent in Android?

问题

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

enter image description here

使用非自定义意图选择器时不会出现这种情况:

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

我尝试了什么

我试图改变意图中的各种事情,但没有任何运气。我也试图找出意图的顺序是什么(因为它可能很重要),但我没有找到它。

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

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

问题

  1. 为什么会这样?我可以以某种方式修复它,同时仍然使用原生意图选择器吗?为什么它只在Android 6及更高版本上发生?

  2. 如何为每个项目添加正确的名称,例如,我看到“twitter”两次,但其他应用程序确实显示了正确的名称(如qr-code-scanner)?

  3. 是否可以保留应用程序订购方式的原生行为,如使用显示意向选择器的简单方式所示?也许以他们应该订购的方式获取应用程序列表?

1 个答案:

答案 0 :(得分:0)

我花了一些时间阅读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);
        }
    }