是否可以排除应用和directshare?

时间:2017-02-28 08:15:12

标签: android android-intent

我想使用directshare功能,但我需要排除应用。

排除部分工作得很好,我只是给选择器一个意图数组,而意图只包括一个特定的应用程序。

但是执行此directshare不起作用。

只有在向选择者提供一个意图时,Directshare似乎才有效。

是否可以排除应用并使用directshare?

代码段:

与意图列表(How to filter specific apps for ACTION_SEND intent (and set a different text for each app))共享:

final Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), "Share with: ");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
activity.startActivity(chooserIntent);

与directshare共享,但不排除:

final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
activity.startActivity(Intent.createChooser(sendIntent, "Share with:"));

1 个答案:

答案 0 :(得分:1)

我遇到了与Direct Share相同的问题,发现它似乎只适用于传递给createChooser()的目标意图。

我的kludgy解决方法是查找"com.android.mms"并将该意图传递给createChooser()targetedShareIntents数组中的其他人,这意味着至少Direct Share适用于短信。

对于某些应用,请注意不要在targetedShareIntents中设置类名,这意味着您最终会在选择器中出现Android系统。

对我而言,这个解决方案还不够好,而且我倾向于不从列表中排除我自己的应用程序。希望我的努力能带领某人做得更好。

以下代码是对此处示例的变体: Custom filtering of intent chooser based on installed Android package name

我在这里看到:http://stackoverflow.com/a/23036439 saulpower可能有更好的解决方案,但我无法使用我的UI。

private void shareExludingApp(Intent intent, String packageNameToExclude, String title) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
    Intent directShare = null;
    if (!resInfo.isEmpty()) {
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(intent);
            if (!info.activityInfo.packageName.startsWith(packageNameToExclude)) {
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShare.setClassName(info.activityInfo.packageName,
                        info.activityInfo.name);
                if (directShare == null && info.activityInfo.packageName.equals("com.android.mms")) {
                    directShare = targetedShare;
                } else {
                    targetedShareIntents.add(targetedShare);
                }
            }
        }
    }
    if (targetedShareIntents.size() > 0) {
        if (directShare == null) {
            directShare = targetedShareIntents.remove(0);
        }
        Intent chooserIntent = Intent.createChooser(directShare, title);
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                targetedShareIntents.toArray(new Parcelable[] {}));
        startActivity(chooserIntent);
    }
    else {
        startActivity(Intent.createChooser(intent, title));
    }
}

用法:

shareExludingApp(intent, getPackageName(), "Share via");
相关问题