Android,如何过滤社交共享只留下Facebook和Twitter?

9
在我的项目中,我需要将一些信息分享到 Facebook 和 Twitter 上。目前,当您使用以下代码时,安卓会提供您手机上所有社交网络的列表。
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,是否可以过滤该列表以只显示这两个网站?

enter image description here


也许你不能直接在Intent中完成这个操作,但我相信你可以实现自己的对话框来完成。 - Huang
3个回答

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

3

您可以查询与意图匹配的活动,然后过滤 Twitter 和 Facebook 应用程序的包名称,因为应用程序的包名称永远不会更改。然后将这些结果放入自定义对话框中。

使用类似以下内容的东西,您可以通过包名称过滤结果:

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

谢谢亲爱的Ken,因此它显示无法过滤列表。我会测试你的建议,稍后告诉结果。再次感谢。 - Hesam

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

不要忘记将此插入到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>

希望这能帮到你!


2
我认为他的意思是如何在其中不包括Gmail和其他应用程序,只有Facebook和Twitter。 - Ken Fehling
谢谢你,亲爱的Mike。这段代码和我的一样。正如Ken所提到的,我需要过滤列表,只保留那两个社交网络。 - Hesam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接