You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android平台Facebook已弃用ShareLinkContent函数的替代方案是什么?

Replacement for Deprecated ShareLinkContent in Android Facebook SDK

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:

  1. Create a SharePhoto (if you need to include an image)
    If your share includes a custom image URL, build a SharePhoto object first:

    SharePhoto sharePhoto = new SharePhoto.Builder()
        .setImageUrl(Uri.parse("https://cdn-images-1.medium.com/fit/t/800/240/1*jZ3..."))
        .build();
    
  2. 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();
    
  3. Trigger the Share Dialog
    Use the ShareDialog just 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—ShareMediaContent works perfectly for plain link shares too.
  • Ensure you’re using the latest version of the Facebook SDK in your build.gradle to access these classes.
  • For programmatic sharing (without the dialog), you can use the Graph API with GraphRequest, but ShareMediaContent is the go-to for user-initiated shares via the native Facebook dialog.

内容的提问来源于stack exchange,提问作者Sudheesh Mohan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:18:19