自定义意图选择器-为什么在Android 6上显示空白单元格? [英] Custom intent-chooser - why on Android 6 does it show empty cells?

查看:109
本文介绍了自定义意图选择器-为什么在Android 6上显示空白单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望展示本机的意图选择器,同时能够对其进行一些自定义.

I wish to show the native intent-chooser, while having the ability to customize it a bit.

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

For this, I've found the next StackOverflow thread:

如何在Android中自定义共享意图?

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

Thing is, when I use the suggested code on Android 5.x and below, everything seems to be fine, but when I use it on Android 6.0.1 (tested on Nexus 5 and emulator, when having multiple apps to share content with) , I get empty cells and sometimes even empty app names, as such:

在使用非定制的intent-chooser时不会出现:

This doesn't appear when using the non-customized intent-chooser:

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

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

Seeing the solutions, I've created the next code:

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

我尝试过的

我试图改变各种意图,但是没有任何运气.我还试图找出意图的顺序(因为这很重要),但我没有找到.

What I've tried

I've tried to change various things in the intents, but without any luck. I've also tried to find out what is the order that the intents are supposed to be in (because maybe it's important), but I didn't find it.

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

Lastly, I've decided to post about it to Google, assuming this is a bug:

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

  1. 为什么会发生?我可以在仍使用本机意图选择器的情况下以某种方式解决此问题吗?它怎么会只在Android 6及更高版本上发生?

  1. Why does it occur? Can I fix it somehow, while still using the native intent-chooser? How come it occurs only on Android 6 and above?

如何在其中为每个项目放置正确的名称,例如,我两次看到"twitter",而其他应用程序确实显示了正确的名称(例如qr-code-scanner之一)?

How can I put the correct name for each item there, as I see "twitter" twice, for example, yet other apps do show the correct name (like the one of the qr-code-scanner)?

是否可以保持使用应用程序订购方式的本机行为,如使用显示意图选择器的简单方法所示?也许以应有的方式获得应用列表?

Is it possible to keep the native behavior of how to order of apps, as shown using the simple way of showing the intent-chooser? Maybe get the list of apps the way they are supposed to be ordered ?

推荐答案

我花一些时间阅读ChooserActivityResolverActivity以及解决这些问题.

I spend some time to read ChooserActivity and ResolverActivity and kind of solving thoese problems.

    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个项目,而不是全部显示. 此处 再次问到此.

seems on Android Q (10 - API 29) this is broken, and will show just up to 2 items instead of all of them. Asked about this again here.

此处 ,提供了一个选择要从共享中排除哪些项目的示例,应该适用于所有Android版本.

made a sample of choosing which items to exclude from sharing, here, which should work for all Android versions.

这篇关于自定义意图选择器-为什么在Android 6上显示空白单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆