[英]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.