从Android应用程序中打开Facebook页面?

175

从我的Android应用程序中,我想要在官方Facebook应用程序中打开一个链接到Facebook个人资料的链接(如果已经安装该应用程序)。对于iPhone设备,存在fb:// URL方案,但是在我的Android设备上尝试相同的事情会抛出ActivityNotFoundException异常。

有没有可能从代码中打开官方Facebook应用程序的个人资料页面?


3
在Facebook开发者论坛上有一个问题没有得到答复: http://forum.developers.facebook.net/viewtopic.php?pid=255227 (这里添加说明是因为它显示了没有明显的答案,或许有一天会在那里得到答复……) - James Moore
你可以从这里看到我的答案。 - Mohammad Sommakia
将 FACEBOOK 应用程序定向到我的页面始终是一个很大的困难。Facebook 支持并没有提供简单的示例代码,除了一直更改路径之外,也没有提供易于使用的支持。因此,我决定从我的应用程序中删除 Facebook 链接。 - Itapox
31个回答

238

这个方法适用于最新版本:

  1. 前往 https://graph.facebook.com/<user_name_here>(例如https://graph.facebook.com/fsintents
  2. 复制您的 ID
  3. 使用此方法:

    public static Intent getOpenFacebookIntent(Context context) {
    
       try {
        context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
        return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/<id_here>"));
       } catch (Exception e) {
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/<user_name_here>"));
       }
    }
    
    这将打开Facebook应用程序(如果用户已经安装了该应用程序)。否则,它将在浏览器中打开Facebook。
    编辑:自版本11.0.0.11.23(3002850)以来,Facebook App不再支持这种方式,有另一种方法,请查看Jared Rummler下面的响应。

1
谢谢你,这帮了我很大的忙!现在如果有一种方法可以在 Twitter 上做到这一点就好了。 - Evan R.
3
iOS上可用的URL schemes列表请参见http://wiki.akosma.com/IPhone_URL_Schemes,其中一些也应该适用于Android。 - FrancescoR
7
这是那个问题的答案:使用“页面”代替“个人资料”- http://stackoverflow.com/a/15872383/1433187 - Khobaib
1
请注意,fb://profile/在Facebook应用程序v1.3.2上不受支持,该版本已包含在Nexus One 2.3.6中。 - Mark
3
DjHacktorReborn是正确的,从Facebook v11开始,fb://profile/<fpid>和fb://page/<fpid>都无法使用。 - sam
显示剩余12条评论

170

在Facebook版本11.0.0.11.23(3002850)中,fb://profile/fb://page/不再起作用。我反编译了Facebook应用程序并发现您可以使用fb://facewebmodal/f?href=[YOUR_FACEBOOK_PAGE]。以下是我一直在生产中使用的方法:

/**
 * <p>Intent to open the official Facebook app. If the Facebook app is not installed then the
 * default web browser will be used.</p>
 *
 * <p>Example usage:</p>
 *
 * {@code newFacebookIntent(ctx.getPackageManager(), "https://www.facebook.com/JRummyApps");}
 *
 * @param pm
 *     The {@link PackageManager}. You can find this class through {@link
 *     Context#getPackageManager()}.
 * @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 = Uri.parse(url);
  try {
    ApplicationInfo applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
    if (applicationInfo.enabled) {
      // https://dev59.com/62445IYBdhLWcg3wia2V#24547437
      uri = Uri.parse("fb://facewebmodal/f?href=" + url);
    }
  } catch (PackageManager.NameNotFoundException ignored) {
  }
  return new Intent(Intent.ACTION_VIEW, uri);
}

4
@DjHacktorReborn,您不应再使用ID,而应该使用普通链接。 - Jared Rummler
2
@DjHacktorReborn 刚刚检查过了,在 Facebook 版本 13.0.0.13.14 上可以正常工作。 - Jared Rummler
1
@AbdulWasae 它已经在数百万次下载的应用程序中运行良好。如果您有更好的解决方案,请发帖分享,而不是对一个可行的解决方案进行贬低。 - Jared Rummler
4
有人可以确认一下,这个还能用吗?(截至2019年6月)。我仍然收到“ActivityNotFoundException”错误。 - nVxx
10
这会打开Facebook页面,但与常规的FB页面不同,我们看不到“赞”按钮、个人资料和封面照片等。它直接打开联系信息并显示页面上的所有帖子。 - Neeraj Dwivedi
显示剩余19条评论

39

这样不是更容易吗?比如在一个onClickListener内部吗?

try {
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/426253597411506"));
    startActivity(intent);
} catch(Exception e) {
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/appetizerandroid")));
}

注:从http://graph.facebook.com/[用户名]获取您的id(大数字)


我同意,这样做更好,因为它不会对Facebook应用程序包名称或意图过滤器做任何假设。但出于同样的原因,它也更糟,因为其他应用程序可能还指定了匹配的意图过滤器。这取决于你想要什么。也许你可以添加intent.setPackageName("com.facebook.katana")来确保不调用其他应用程序。 - Mark
但它会打开浏览器并显示Facebook数据,如何使用Facebook应用程序(调用Facebook应用程序)? - Mikheil Zhghenti

32

对于 Facebook 页面:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/" + pageId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + pageId));
}

Facebook个人资料:

try {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + profileId));
} catch (Exception e) {
    intent =  new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + profileId));
}

因为没有一个答案指出了差异。

在Nexus 4上使用Facebook v.27.0.0.24.15和Android 5.0.1进行了测试。


嗨,如何区分个人档案和页面? - alvarodoune
1
个人资料是为私人而设,而页面则是为公司、地点、品牌等而设。请参见:https://www.facebook.com/help/217671661585622 [标题:页面与个人资料有何不同?] - Murmel
意图 = 新 Intent(Intent.ACTION_VIEW, Uri.parse("fb://note/" + facebookNoteId)); - voodoo98
1
要查找公共页面、活动、个人资料的ID,您可以使用FindMyFbId - Carlos Zerga
Facebook应用程序中的个人资料无法打开。 - Hammad Nasir
但它会打开已登录用户的个人资料,如果我们有不同的app_scoped_id,则仍然会打开已登录用户的个人资料。 - Janardhan R

29

以下是2016年完成此操作的方法。它非常有效,并且非常易于使用。

在研究Facebook发送的电子邮件如何打开Facebook应用程序后,我发现了这一点。

// e.g. if your URL is https://www.facebook.com/EXAMPLE_PAGE, you should
// put EXAMPLE_PAGE at the end of this URL, after the ?
String YourPageURL = "https://www.facebook.com/n/?YOUR_PAGE_NAME";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(YourPageURL));

startActivity(browserIntent);

1
完美运行,即使在2016年也是如此。解决了在比利时浏览Facebook时的一个问题(当未登录时禁用cookie)。 - doubotis
3
使用此功能时,在 Facebook 应用程序中引用的 Facebook 页面标题将消失,例如页面个人资料图片以及页面/活动/见解选项卡。 - Daniel Persson
如果您有页面名称(如所述),则此方法有效,但如果您有数字页面ID,则无效。 - Eric B.
8
好的,但它没有将用户带到一个干净的页面,让他们轻松地喜欢我的页面...仍在寻找解决方案!有任何解决办法吗? - toto_tata
1
有人有@Regis_AG的解决方案吗? - adrianvintu

23

这是做这件事最简单的代码。

public final void launchFacebook() {
        final String urlFb = "fb://page/"+yourpageid;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(urlFb));

        // If a Facebook app is installed, use it. Otherwise, launch
        // a browser
        final PackageManager packageManager = getPackageManager();
        List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
        if (list.size() == 0) {
            final String urlBrowser = "https://www.facebook.com/"+pageid;
            intent.setData(Uri.parse(urlBrowser));
        }

        startActivity(intent);
    }

这个没用。当我设置链接"https://www.facebook.com/pages/"+pageid"时。 - BSKANIA
省略“pages/”部分->使用“facebook.com/”+页面ID - Murmel
仍然像今天一样工作!非常感谢。我用这个替换了这部分代码:""https://www.facebook.com/pages/"+pageid;",改成了这个:""https://www.facebook.com/"+pageid;"。 - Nabil Ait Brahim
@naitbrahim 谢谢您让我们知道它仍然有效!我已经编辑了答案。顺便说一下,这个答案是在2013年发布的 :) 很高兴它能帮到您。 - MBH

14

一种更可重用的方法。

这是我们在大多数应用程序中通常使用的功能。因此,这里提供了一个可重用的代码片段来实现它。

(和其他答案类似,只是为了简化并使实现可重用而发布)

"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 facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);

这就是你所需要的。


它抛出了 "android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=fb://page/#### }"。 - Rajbir Shienh

10

为了完成这个任务,我们需要获取“Facebook页面ID”,您可以通过以下步骤获得:

  • 进入该页面并点击“关于”。
  • 进入“更多信息”部分。

在此输入图像说明

要在指定的个人资料页面上打开Facebook应用程序,

您可以执行以下操作:

 String facebookId = "fb://page/<Facebook Page ID>";
  startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));

或者,当Facebook应用程序未安装时,您可以进行验证,然后打开Facebook网页。

String facebookId = "fb://page/<Facebook Page ID>";
String urlPage = "http://www.facebook.com/mypage";

     try {
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId )));
        } catch (Exception e) {
         Log.e(TAG, "Application not intalled.");
         //Open url web page.
         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
        }

1
我在关于选项卡中没有看到这个“页面ID”。它是私有的吗? - RoCkDevstack
@RoCkRoCk 这样获取:curl -skA“Mozilla/5.0”https://www.facebook.com/mypage/ | grep -oE“fb://[^\"]+” - ubuntudroid

9

这个被Pierre87在FrAndroid论坛上逆向工程,但我找不到任何官方描述它的地方,所以它必须被视为未记录并有可能随时停止工作:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName("com.facebook.katana", "com.facebook.katana.ProfileTabHostActivity");
intent.putExtra("extra_user_id", "123456789l");
this.startActivity(intent);

是的,解决方案已经停止工作了。错误是:“java.lang.SecurityException?:Permission Denial:starting Intent {flg = 0x10080000 cmp = com.facebook.katana / .ProfileTabHostActivity?(has extras)} from ProcessRecord?”ProfileTabHostActivity活动在AndroidManifest中没有android:exported标志,因此默认情况下android:exported =“false”对于此活动。结果,其他应用程序无法启动ProfileTabHostActivity。唯一的希望是FB开发人员将在未来版本的FB应用程序中修复此问题。 - dvpublic
我认为解决方法是关闭这个“意外的API”。 - Vaiden

7
尝试这段代码:
String facebookUrl = "https://www.facebook.com/<id_here>";
        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 {
                Uri uri = Uri.parse("fb://page/<id_here>");
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        } catch (PackageManager.NameNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookUrl)));
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接