Android,如何通过Facebook和Twitter过滤社交分享?

Hes*_*sam 8 twitter android facebook social-networking

在我的项目中,我需要将一些信息分享到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:"));
}
Run Code Online (Sandbox Code Playgroud)

要求只是在此列表中显示Facebook和Twitter.是否有可能过滤此列表才能拥有这两个?

在此输入图像描述

Piy*_*ush 8

    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();
         }
    }
Run Code Online (Sandbox Code Playgroud)


小智 0

我不知道这是否是你的意思,但你可以使用 Android 内置共享菜单...

您可以使用 Intents 将 URL 共享到Facebook、Twitter、Gmail等(只要您的设备上安装了这些应用程序):

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
i.putExtra(Intent.EXTRA_TEXT, "http://www.url.com");
startActivity(Intent.createChooser(i, "Share URL"));
Run Code Online (Sandbox Code Playgroud)

不要忘记将其插入到 Manifest.xml 中:

<activity android:name="ShareLink">
                <intent-filter>
                    <action android:name="android.intent.action.SEND" />
                    <category android:name="android.intent.category.DEFAULT" />
                    <data android:mimeType="text/plain" />
                </intent-filter>
            <meta-data/>
            </activity>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • 我认为他的意思是他怎么能没有 Gmail 和其他应用程序,而只有 Facebook 和 Twitter。 (2认同)