android - 从 android 应用程序打开 facebook 页面(在 facebook 版本 > v11 中)

标签 android facebook

我曾经使用以下代码从我的应用程序打开我的 facebook 页面,但是从 2014 年 6 月 21 日发布的 facebook v11.0.0.11.23 开始,这不再有效,知道如何在新的 facebook 应用程序中打开页面? 要注意它现在打开 facebook 应用程序但没有打开指定的页面,它在最新更新之前工作得很好

public void openFacebookPage() {
    Intent intent = null;
    try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id here>"));
        //tried this also
        //intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/<id here>"));
    } catch (Exception e) {
        intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<name here>"));
    }
    context.startActivity(intent);
}

最佳答案

在 Facebook 版本 11.0.0.11.23 (3002850) 中,不再支持 fb://profile/和 fb://page/。我反编译了 Facebook 应用程序,并得出了以下解决方案:

String facebookUrl = "https://www.facebook.com/JRummyApps";
try {
    int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
    if (versionCode >= 3002850) {
        Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
        startActivity(new Intent(Intent.ACTION_VIEW, uri));;
    } else {
        // open the Facebook app using the old method (fb://profile/id or fb://page/id)
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/336227679757310")));
    }
} catch (PackageManager.NameNotFoundException e) {
    // Facebook is not installed. Open the browser
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
}

编辑: 已经有一段时间了,似乎不再支持 fb://profile 和 fb://page。以下是我在生产中一直使用的方法:

/**
 * Intent to open the official Facebook app. If the Facebook app is not installed then the
 * default web browser will be used.</p>
 * 
 * Example usage:</p>
 * <code>newFacebookIntent(context.getPackageManager(), "https://www.facebook.com/JRummyApps");</code></p>
 * 
 * @param pm
 *            Instance of the {@link PackageManager}.
 * @param url
 *            The full URL to the Facebook page or profile.
 * @return An intent that will open the Facebook page/profile.
 */
public static Intent newFacebookIntent(PackageManager pm, String url) {
    Uri uri;
    try {
        pm.getPackageInfo("com.facebook.katana", 0);
        // http://stackoverflow.com/a/24547437/1048340
        uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    } catch (PackageManager.NameNotFoundException e) {
        uri = Uri.parse(url);
    }
    return new Intent(Intent.ACTION_VIEW, uri);
}

关于android - 从 android 应用程序打开 facebook 页面(在 facebook 版本 > v11 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24526882/

相关文章:

android - 通过 Rails API 使用 Facebook 进行身份验证

android - 在 Activity 停止和恢复时保存 SurfaceView 的状态

android - 如何捕捉 ACTION_MEDIA_BUTTON 的按住(长按)

Android:检测 Activity/应用程序更改事件

java - 计算器应用程序的定制键盘

ios - 获取 Facebook 好友列表

android - Facebook Android SDK FeedDialog 标签

ios - Unity Facebook SDK 7.0.2 在 iOS 中编译失败

java - Android 中的单例与应用程序上下文?

iphone - 如何从 Facebook 登录重定向到 iphone 应用程序