在 Android 上的 Facebook 应用程序(如果已安装)中打开 Facebook 页面 [英] Open Facebook Page in Facebook App (if installed) on Android
问题描述
我想从我的 Android 应用程序打开我的 Facebook 页面,如果 Facebook 应用程序可用 - 如果没有:此页面应在默认浏览器中打开.
I would like to open my Facebook page from my Android app, if the Facebook app is available - if not: this page should be open in the default browser.
为此,我尝试了以下代码:
for this i tried the following code:
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/MY_PAGE_ID"));
startActivity(intent);
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/MY_PAGE_NAME")));
}
问题是:我这里有一台装有最新版 Facebook 的 Android 设备.如果我想从我的应用程序打开 Facebook 页面,Facebook 应用程序将打开,但没有我的页面.
Problem is: I have here an Android device with the newest version of Facebook. If I would like to open from my app the Facebook page, the Facebook app will open , but without my page.
我只看到消息:
无法加载时间线.
怎么了?
推荐答案
"fb://page/
不适用于较新版本的 FB 应用程序.您应该使用 fb://facewebmodal/f?href=
对于较新的版本.
"fb://page/
does not work with newer versions of the FB app. You should use fb://facewebmodal/f?href=
for newer versions.
这是一个完整的工作代码,目前存在于我的一个应用中:
This is a full fledged working code currently live in one of my apps:
public static String FACEBOOK_URL = "https://www.facebook.com/YourPageName";
public static String FACEBOOK_PAGE_ID = "YourPageName";
//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://page/" + FACEBOOK_PAGE_ID;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL; //normal web url
}
}
如果已安装,此方法将返回正确的应用 url,如果未安装,则返回 web url.
This method will return the correct url for app if installed or web url if app is not installed.
然后开始一个意图如下:
Then start an intent as follows:
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
这就是你所需要的.
这篇关于在 Android 上的 Facebook 应用程序(如果已安装)中打开 Facebook 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!