Android 在Facebook应用程序中打开Facebook帖子
我想在我的应用程序中加入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/" +
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);