android - 在 Android 上的 Facebook 应用程序(如果已安装)中打开 Facebook 页面

标签 android facebook android-intent

如果 Facebook 应用程序可用,我想从我的 Android 应用程序打开我的 Facebook 页面 - 如果没有:此页面应在默认浏览器中打开。

为此我尝试了以下代码:

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 应用将打开,但没有我的页面。

我只看到消息:

Trouble Loading Timeline.

怎么了?

最佳答案

"fb://page/ 不适用于较新版本的 FB 应用程序。对于较新版本,您应该使用 fb://facewebmodal/f?href=版本。

这是一个完整的工作代码,目前存在于我的一个应用程序中:

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。

然后按如下方式启动一个 Intent :

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

这就是你所需要的。

关于android - 在 Android 上的 Facebook 应用程序(如果已安装)中打开 Facebook 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34564211/

相关文章:

android - 如何获得一个文本可以增长和椭圆化的布局,但不会吞噬布局上的其他元素

java - Android - 以编程方式设置 "android:layout_column="1""?

Facebook API : Update Group Description

Android:使用提供的 session Cookie 打开浏览器

android - 无法在 ActivityGroup 持有的 PreferenceScreen 中加载 ListPreference 对话框

java - 如何从 PHP 服务器上的 android 获取值

ios - 如何使用 iPhone 为 Facebook 创建新相册

iphone - iOS UIWebView 中的 Facebook 评论

android - Activity 忽略 Intent 的额外字段

android - onActivityResult 永远不会被调用 Phonegap Cordova