Android平台Facebook已弃用ShareLinkContent函数的替代方案是什么?
Hey there! I totally get the frustration when a familiar API gets deprecated—let’s walk through the official replacement for ShareLinkContent step by step.
Starting with Facebook SDK 12.x and above, ShareLinkContent has been marked as deprecated, and the recommended alternative is ShareMediaContent. This class gives you the flexibility to share links alongside media (like photos) while retaining all the core properties you used with ShareLinkContent.
Step-by-Step Implementation
Here’s how to replicate your original share functionality using the new API:
Create a
SharePhoto(if you need to include an image)
If your share includes a custom image URL, build aSharePhotoobject first:SharePhoto sharePhoto = new SharePhoto.Builder() .setImageUrl(Uri.parse("https://cdn-images-1.medium.com/fit/t/800/240/1*jZ3...")) .build();Build the
ShareMediaContent
Now assemble your share content with title, description, target URL, and the photo you created:ShareMediaContent shareContent = new ShareMediaContent.Builder() .addMedium(sharePhoto) // Add the photo (skip this line if no image is needed) .setContentUrl(Uri.parse("https://justa128.github.io/dubai-tour-guide/landingpage/")) .setContentTitle("Planning a trip to Dubai?") .setContentDescription("Make sure you visit unique attractions recommended by the local people!") .build();Trigger the Share Dialog
Use theShareDialogjust like you did before to present the share interface to the user:ShareDialog.show(yourActivity, shareContent);
Key Notes
- If you don’t need to include an image, simply omit the
.addMedium(sharePhoto)line—ShareMediaContentworks perfectly for plain link shares too. - Ensure you’re using the latest version of the Facebook SDK in your
build.gradleto access these classes. - For programmatic sharing (without the dialog), you can use the Graph API with
GraphRequest, butShareMediaContentis the go-to for user-initiated shares via the native Facebook dialog.
内容的提问来源于stack exchange,提问作者Sudheesh Mohan

