Intent createChooser停止代码在Marshmallow中不起作用

时间:2015-12-17 16:16:16

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

我在Marshmallow之前的应用程序中有一个代码,它只选择了一些已实现Intent.ACTION_SEND的已安装应用程序,并仅显示Intent.createChooser。这是完整的代码:

Intent actionSendIntent = new Intent();
actionSendIntent.setAction(Intent.ACTION_SEND);
actionSendIntent.setType("text/plain");
List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(actionSendIntent, 0);

List<Intent> shareIntents = new ArrayList<Intent>();
if(!resInfos.isEmpty()){
    for(ResolveInfo res : resInfos){
        final String packageName = res.activityInfo.packageName;
        if (packageName.contains("com.twitter.android") ||
                packageName.contains("com.facebook.katana") ||
                packageName.contains("com.facebook.lite") ||
                packageName.contains("com.whatsapp")) {
            final Intent intent = new Intent();
            intent.setComponent(new ComponentName(packageName, res.activityInfo.name));
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "text");
            intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
            intent.setPackage(packageName);
            shareIntents.add(intent);
        }
    }
}
Intent chooserIntent = Intent.createChooser(shareIntents.remove(0), shareText);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, shareIntents.toArray(new Parcelable[]{}));
startActivity(Intent.createChooser(chooserIntent, shareText));

此代码在Marshmallow中不起作用,并将Android系统显示为唯一可用的选项...有谁知道为什么?

0 个答案:

没有答案