在Facebook应用程序中打开Facebook帖子

3

我希望在我的应用程序中包含Facebook的故事,这样当用户点击一篇文章时,它会在Facebook的本机应用程序中打开(如果存在)。 我已经尝试了下面的代码,但它似乎非常过时。

 Intent viewIntent;
        try {
            getActivity().getPackageManager().getPackageInfo("com.facebook.katana", 0);
            String uri = "fb://post/" + post.getId();
            viewIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(uri));
        } catch (Exception e) {
            String url = "https://www.facebook.com/" + PAGE_ID
                    + "/posts/" + post.getId;
            viewIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(url));
        }
        startActivity(viewIntent);
1个回答

0

您正在使用的 URI 仅适用于旧版本的 Facebook 应用程序。请使用

public static String FACEBOOK_URL = "https://www.facebook.com/" + PAGE_ID
                + "/posts/" + post.getId;

//method to get the right URL to use in the intent
public String getFacebookPageURL(Context context) {
    PackageManager packageManager = context.getPackageManager();
    try {
        int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
        if (versionCode >= 3002850) { //newer versions of fb app
            return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
        } else { //older versions of fb app
            return "fb://post/" + post.getId();;
        }
    } catch (PackageManager.NameNotFoundException e) {
        return FACEBOOK_URL; //normal web url
    }
}

那么

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

这就是你所需要的全部


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