我已经下载了 Facebook SDK 并进行了正确设置。一切都很完美。我可以毫无问题地分享链接或更新我的状态。现在我想做点别的。我想以这样的方式集成 Share Intent 和 Facebook SDK,当我在我的应用程序中单击共享按钮时,应该调用共享 Intent 并显示应用程序列表。
现在,如果从列表中选择了 Facebook,它应该打开我在应用程序上应用的 Facebook SDK 共享方法,但没有共享 Intent 。如果选择了任何其他应用程序(Gmail/Twitter/SMS 等),共享 Intent 应正常调用相应的 Intent 。
到目前为止,我尝试了以下代码:
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;
}
}
我尝试了这段代码,但没有成功。 Share Intent 被调用,当我点击 Facebook 时,它会打开一个 Facebook 状态更新页面,因为我希望它调用我设置的 Facebook SDK 方法并使用预定义链接运行该方法。
我怎样才能做到这一点?
最佳答案
我使用 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{
}
}
关于android - Sharing Intent 结合 Facebook SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23674042/