繁体   English   中英

在Facebook应用中打开Facebook帖子

[英]Open Facebook post in Facebook app

我想在我的应用程序中包含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);

您使用的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);

这就是你需要的一切

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM