0

I want to use the directshare feature, but i need to exclude apps.

The excluding part works pretty well, i am just giving an array of intents to the chooser, while the intents are only including one specific application.

But doing this directshare does not work.

Directshare only seems to be working when giving exactly one intent to the chooser.

Is it possible to exclude apps and use directshare?

Code Snippets:

Sharing with a list of intents (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);

Sharing with directshare, but no excluding:

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:"));
Community
  • 1
  • 1
PhilipB
  • 329
  • 2
  • 16

1 Answers1

1

I ran into the same problem with Direct Share, and found that it only seems to work for the target intent passed to createChooser().

My kludgy work-around was to lookup "com.android.mms" and pass that intent to createChooser() and the others in the targetedShareIntents array, which means that at least Direct Share works for text messages.

Note for some apps, not setting the class name in targetedShareIntents means you end up with Android System appearing in chooser instead.

For me this solution isn't good enough, and I'm leaning towards not excluding my own app from the list. Hopefully my efforts will lead someone to something better.

Code below is a variation on examples found here: Custom filtering of intent chooser based on installed Android package name

I see here: http://stackoverflow.com/a/23036439 that saulpower may have a better solution, but I can't get it to work with my 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));
    }
}

Usage:

shareExludingApp(intent, getPackageName(), "Share via");