Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, How to filter social sharing for having just Facebook and Twitter?

In my project I need to share some information into Just Facebook and Twitter. Currently when you use following codes, android offers list of all social networks that you have in your mobile phone.

public void share(String subject,String text) {
 final Intent intent = new Intent(Intent.ACTION_SEND);

 intent.setType("text/plain");
 intent.putExtra(Intent.EXTRA_SUBJECT, subject);
 intent.putExtra(Intent.EXTRA_TEXT, text);
 startActivity(Intent.createChooser(intent, "Share with:"));
}

The requirement is just showing Facebook and Twitter in this list. Is it possible to filter this list in order to have these two?

enter image description here

like image 558
Hesam Avatar asked Mar 02 '12 03:03

Hesam


2 Answers

    Button btnshare=(Button)rootView.findViewById(R.id.btnshare);
        btnshare.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Share(getShareApplication(),"Hello Text Share");
            }
        });

    private List<String> getShareApplication(){
        List<String> mList=new ArrayList<String>();
        mList.add("com.facebook.katana");
        mList.add("com.twitter.android");
        mList.add("com.google.android.gm");
        return mList;

    }
    private void Share(List<String> PackageName,String Text) {
        try
        {
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("text/plain");
            List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
            if (!resInfo.isEmpty()){
                for (ResolveInfo info : resInfo) {
                    Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
                    targetedShare.setType("text/plain"); // put here your mime type
                    if (PackageName.contains(info.activityInfo.packageName.toLowerCase())) {
                        targetedShare.putExtra(Intent.EXTRA_TEXT,Text);
                        targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
                        targetedShareIntents.add(targetedShare);
                    }
                }
                Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share");
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
                startActivity(chooserIntent);
            }
        }
        catch(Exception e){
             e.printStackTrace();
         }
    }
like image 76
Piyush Avatar answered Oct 03 '22 01:10

Piyush


You could query for the activities that match the intent and then filter for the package names of the Twitter and Facebook apps since the package name of an app never changes. Then put these results in a custom dialog.

Using something like this you could filter the results by package name:

final PackageManager pm = context.getPackageManager();
final Intent intent = new Intent(Intent.ACTION_SEND);
List<ResolveInfo> riList = pm.queryIntentActivities(intent, 0);
for (ResolveInfo ri : riList) {
    ActivityInfo ai = ri.activityInfo;
    String pkg = ai.packageName;
    if (pkg.equals("com.facebook.katana") || pkg.equals("com.twitter.android")) {

        // Add to the list of accepted activities.

        // There's a lot of info available in the
        // ResolveInfo and ActivityInfo objects: the name, the icon, etc.

        // You could get a component name like this:
        ComponentName cmp = new ComponentName(ai.packageName, ai.name);
    }
}
like image 32
Ken Fehling Avatar answered Oct 03 '22 00:10

Ken Fehling