Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude your own app from the Share menu?

Tags:

The app has an intent filter to allow it to appear in the share menu in other applications via ACTION_SEND intents. The app itself also has a share menu using ACTION_SEND and createChooser(), and my app appears in the list. Since they are already in my app it seems strange to have them be able to share back to itself.

Is there a way for my app not to appear in the list if it's being called from my app?

like image 440
cottonBallPaws Avatar asked Oct 31 '10 19:10

cottonBallPaws


People also ask

How do I make my Android app appear in the share list of another app?

In order to do this, you need to know the Intent that the application is creating and create an IntentFilter that will add your application to the specific list. The application probably uses a specific action name that you could hook to. Or it could be looking for applications accepting a certain type of file.


2 Answers

Here goes your solution. If you want to exclude your own app you can change "packageNameToExclude" with ctx.getPackageName()

public static Intent shareExludingApp(Context ctx, String packageNameToExclude, String imagePath, String text) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/*");
    List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(text,new File(imagePath)), 0);
    if (!resInfo.isEmpty()) {
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = createShareIntent(text,new File(imagePath));

            if (!info.activityInfo.packageName.equalsIgnoreCase(packageNameToExclude)) {
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShareIntents.add(targetedShare);
            }
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
                "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                targetedShareIntents.toArray(new Parcelable[] {}));
        return chooserIntent;
    }
    return null;
}

private static Intent createShareIntent(String text, File file) {
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/*");
    if (text != null) {
        share.putExtra(Intent.EXTRA_TEXT, text);
    }
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    return share;
}
like image 196
Addev Avatar answered Sep 22 '22 13:09

Addev


Is there a way for my app not to appear in the list if it's being called from my app?

Not via createChooser(). You can create your own chooser-like dialog via PackageManager and queryIntentActivities() and filter yourself out that way, though.

like image 41
CommonsWare Avatar answered Sep 22 '22 13:09

CommonsWare