Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Facebook应用程序中打开Facebook帖子_Android_Facebook - Fatal编程技术网

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

Android 在Facebook应用程序中打开Facebook帖子,android,facebook,Android,Facebook,我想在我的应用程序中加入Facebook的故事,这样当用户点击帖子时,它就会在Facebook的原生应用程序(如果存在的话)中打开。 我已经尝试了下面的代码,但它似乎非常过时 Intent viewIntent; try { getActivity().getPackageManager().getPackageInfo("com.facebook.katana", 0); String uri = "fb://post/" +

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