How to exclude a specific application from ACTION_SEND Intent?

后端 未结 6 527
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 04:55

I have used the following codes to exclude facebook app from my app chooser:

 List targetedShareIntents = new ArrayList();
    In         


        
6条回答
  •  离开以前
    2020-12-19 05:28

    I had to exclude facebook apps as they had a broken share intent (something about user originality) using Intent#EXTRA_EXCLUDE_COMPONENTS I resorted to this:

            Intent intent = new Intent(Intent.ACTION_SEND);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                intent.setTypeAndNormalize("text/plain");
            } else {
                intent.setType("text/plain");
            }
            intent.putExtra(Intent.EXTRA_TEXT, "Text to share");
    
            Intent chooser = Intent.createChooser(intent, "Share Text");
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
                ArrayList targets = new ArrayList<>();
    
                // remove facebook which has a broken share intent
                for (ResolveInfo candidate : requireActivity().getPackageManager().queryIntentActivities(intent, 0)) {
                    String packageName = candidate.activityInfo.packageName;
                    if (packageName.toLowerCase().contains("facebook")) {
                        targets.add(new ComponentName(packageName, candidate.activityInfo.name));
                    }
                }
                chooser.putExtra(Intent.EXTRA_EXCLUDE_COMPONENTS, targets.toArray(new ComponentName[0]));
            }
    
            startActivity(chooser);
    

    This only works for android N and above

提交回复
热议问题