共享Intent与Facebook SDK结合使用

时间:2014-05-15 09:17:44

标签: android facebook android-intent

我已经下载了Facebook SDK并正确设置了它。一切都很完美。我可以共享链接或更新我的状态没有问题。现在我想做点别的事。我希望以这样的方式集成Share Intent和Facebook SDK,当我点击我的应用程序中的共享按钮时,应该调用共享意图并显示应用程序列表。

现在如果从列表中选择Facebook,它应该打开我在应用程序上应用的Facebook SDK共享方法,而没有共享意图。如果选择任何其他应用程序(Gmail / Twitter / SMS等),共享意图应该正常调用相应的意图。

到目前为止,我尝试了以下代码:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                sharingIntent.setType("text/plain");
                String shareBody = "Here is the share content body";
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
                        "http://somelink.com/somethingelse.php");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

                PackageManager pm = v.getContext().getPackageManager();

                List<ResolveInfo> activityList = 
                        pm.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);

                for (final ResolveInfo app : activityList) {
                    if ((app.activityInfo.name).contains("facebook")) {

                        Log.d("its here:", "goood");

                        FacebookDialog shareDialog = 
                                new FacebookDialog.ShareDialogBuilder((Activity) v.getContext())
                        .setLink("http://somelink.com/somethingelse.php").build();
                        uiHelper.trackPendingDialogCall(shareDialog.present());

                        final ActivityInfo activity = app.activityInfo;
                        final ComponentName name = 
                                new ComponentName(activity.applicationInfo.packageName, activity.name);
                        sharingIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                        sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                        sharingIntent.setComponent(name);
                        v.getContext().startActivity(sharingIntent);
                        break;
                    } else {
                        Log.d("shouldnt be here", "nooo");
                        startActivity(Intent.createChooser(sharingIntent, "Share via"));
                        break;
                    }
                }

我尝试了这段代码,但没有运气。分享意图被调用,当我点击Facebook时,它会打开一个Facebook状态更新页面,我希望它可以调用我设置的Facebook SDK方法并使用预定义的链接运行该方法。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:4)

我使用Facebook SDK 4.5.1,这种方法适用于Facebook SDK 4.0 +

ShareDialog shareDialog;
CallbackManager callbackManager;
onCreateView()

中的

callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);

在您要应用共享对话框的函数或代码中。

if (ShareDialog.canShow(ShareLinkContent.class)) {
        ShareLinkContent linkContent = new ShareLinkContent.Builder()
                .setContentTitle("Ttile")
                .setContentDescription("Description")
                .setContentUrl(Uri.parse("http://somelink.com/somethingelse.php"))
                .build();
        shareDialog.show(linkContent);
     }

你的onActivityResult()应如下所示:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    callbackManager.onActivityResult(requestCode, resultCode, data);
 }

编辑1:用于捕获用户从应用列表中选择Facebook的事件。

List<Intent> targetShareIntents=new ArrayList<Intent>();
    Intent shareIntent=new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/*");

    List<ResolveInfo> resInfos=context.getPackageManager().queryIntentActivities(shareIntent, 0);

    if(!resInfos.isEmpty()){
        for(ResolveInfo resInfo : resInfos){
            String packageName=resInfo.activityInfo.packageName;

            if(packageName.contains("com.facebook.katana")){

              //Facebook sharing code

            }

        }

        if(!targetShareIntents.isEmpty()){
            Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
            context.startActivity(chooserIntent);
        }else{
        }
    }