How to filter specific apps for ACTION_SEND intent (and set a different text for each app)

后端 未结 12 1914
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 01:08

How can you filter out specific apps when using the ACTION_SEND intent? This question has been asked in various ways, but I haven\'t been able to gather a

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:34

    You can try the code below, it works perfectly.

    Here we share to some specific apps, that are Facebook, Messenger, Twitter, Google Plus and Gmail.

    public void shareIntentSpecificApps() {
            List intentShareList = new ArrayList();
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            List resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
    
            for (ResolveInfo resInfo : resolveInfoList) {
                String packageName = resInfo.activityInfo.packageName;
                String name = resInfo.activityInfo.name;
                Log.d(TAG, "Package Name : " + packageName);
                Log.d(TAG, "Name : " + name);
    
                if (packageName.contains("com.facebook") ||
                        packageName.contains("com.twitter.android") ||
                        packageName.contains("com.google.android.apps.plus") ||
                        packageName.contains("com.google.android.gm")) {
    
                    if (name.contains("com.twitter.android.DMActivity")) {
                        continue;
                    }
    
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(packageName, name));
                    intent.setAction(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Your Subject");
                    intent.putExtra(Intent.EXTRA_TEXT, "Your Content");
                    intentShareList.add(intent);
                }
            }
    
            if (intentShareList.isEmpty()) {
                Toast.makeText(MainActivity.this, "No apps to share !", Toast.LENGTH_SHORT).show();
            } else {
                Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share via");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
                startActivity(chooserIntent);
            }
        }
    

提交回复
热议问题