21

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?

cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171

4 Answers4

22

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;
}
dldnh
  • 8,923
  • 3
  • 40
  • 52
Addev
  • 31,819
  • 51
  • 183
  • 302
9

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This there something built in that I can feed my filtered List back into, to create the Share Dialog, or do I have to create my own dialog using the list? If it's the latter how do you get the App's icons to appear in a dialog? Thanks. – cottonBallPaws Oct 31 '10 at 21:30
  • 1
    @littleFluffyKitty: You would use you own dialog (e.g., `AlertDialog.Builder` and `setAdapter()`). `PackageManager` can give you the icons. See http://github.com/commonsguy/cw-advandroid/tree/master/Introspection/Launchalot/ – CommonsWare Nov 01 '10 at 07:04
  • thank you that's really helpful. Do you have any suggestions which is the best way to compare/filter the list returned from queryIntentActivities() against my app? I was thinking to use the package name, but is there a better comparison? – cottonBallPaws Nov 01 '10 at 20:37
  • 2
    @littleFluffyKitty: Package name is almost assuredly your best option. – CommonsWare Nov 01 '10 at 20:43
1

You should use

Intent chooserIntent = Intent.createChooser(new Intent(), "Select app to share");
1

Already Answered here - https://stackoverflow.com/a/74131607/7764015

After updating targetSdkVersion to 31 the older solution will stop working. This below solution will work for all android version

In Manifest file AndroidManidest.xml

(In Android api 31 by default queryIntentActivities() will return empty list to get the desired result from queryIntentActivities you need to add the below query parameter in manifest file.)

<manifest>
   <queries>
      <intent>
         <!--Change the action and data depending on you share intent-->
         <action android:name="android.intent.action.SEND" /> 
         <data android:mimeType="text/*"/>

      </intent>
   </queries>
</manifest>

In the activity file share function

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");

shareIntent.putExtra(Intent.EXTRA_TEXT, "Link");
shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Description")

ArrayList<ComponentName> excludedComponents = new ArrayList<ComponentName>();
PackageManager packageManager = context.getPackageManager();
for (ResolveInfo resolveInfo : packageManager.queryIntentActivities(shareIntent, 0)){
String packageName = resolveInfo.activityInfo.packageName;
//change facebook with whichever app you want to exclude or you can directly search for the specific package name
   if (packageName.contains("facebook")){
   excludedComponents.add(new 
   ComponentName(packageName,resolveInfo.activityInfo.name));
   }
}

Intent intentChooser = Intent.createChooser(shareIntent, "Share");
intentChooser.putExtra(Intent.EXTRA_EXCLUDE_COMPONENTS, excludedComponents.toArray(new Parcelable[]{}));
startActivity(intentChooser);
Priyankchoudhary
  • 786
  • 9
  • 12