在Facebook上打开Facebook页面(如果已安装) [英] Open Facebook Page in Facebook App (if installed) on Android

查看:209
本文介绍了在Facebook上打开Facebook页面(如果已安装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的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")));
}

问题是:
我在这里有一个最新的Android设备版本的Facebook。
如果我想从我的应用程序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.

我只看到消息: / p>

I only see the message:


麻烦加载时间轴。

Trouble Loading Timeline.

什么是错误?

推荐答案

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
        }
    }

此方法将返回正确的如果没有安装应用程序,则应用程序的网址为网址或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);

这就是你需要的。

这篇关于在Facebook上打开Facebook页面(如果已安装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆