在我的项目中,我需要将一些信息共享到Just Facebook和Twitter中。目前,当您使用以下代码时,Android会提供您手机中所有社交网络的列表。

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:"));
}

要求只是在此列表中显示Facebook和Twitter。是否可以过滤此列表以具有这两个列表?

最佳答案

    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();
         }
    }

关于Android,如何仅使用Facebook和Twitter来过滤社交共享?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9527698/

10-10 14:47