是否可以排除应用程序和直接签名



我想使用DirectShare功能,但我需要排除应用程序。

排除零件的运作效果很好,我只是为选择者提供了一系列意图,而意图仅包含一个特定的应用程序。

,但是执行此直接肖像行不通。

直接肖像似乎只有在给出一个选择者的目的时才起作用。

是否可以排除应用程序并使用DirectShare?

代码片段:

共享意图列表(如何过滤action_send意图的特定应用程序(并为每个应用程序设置不同的文本)):

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

我遇到了直接共享的同一问题,发现它似乎仅适用于目标意图传递给createChooser()

我的工作工作是查找"com.android.mms"并将其传递给createChooser()targetedShareIntents数组中的其他意图,这意味着至少直接共享用于文本消息。

注意某些应用程序,不设置targetedShareIntents中的类名称,表示您最终会出现在Chooser中的Android系统。

对我来说,这个解决方案还不够好,我倾向于不将自己的应用程序排除在列表中。希望我的努力能使某人更好。

下面的代码是此处找到的示例的变体:基于安装的Android软件包名称

自定义过滤意向选择器

我在这里看到: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");

最新更新