用 Android 应用打开 Facebook 页面?

从我的安卓应用程序中,我想打开一个链接到官方 Facebook 应用程序中的一个 Facebook ID (当然,如果安装了该应用程序)。对于 iPhone,存在 fb:// URL 方案,但是在我的 Android 设备上尝试同样的方案会抛出一个 ActivityNotFoundException

有没有可能通过代码在 Facebook 官方应用程序中打开一个 Facebook ID?

359287 次浏览

这是 由 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);

这对最新版本有效:

  1. 转到 https://graph.facebook.com/<user_name_here > (例如 https://graph.facebook.com/fsintents)
  2. 复制你的身份证
  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>"));
    }
    }
    

This will open the Facebook app if the user has it installed. Otherwise, it will open Facebook in the browser.

EDIT: since version 11.0.0.11.23 (3002850) Facebook App do not support this way anymore, there's another way, check the response below from Jared Rummler.

这是最简单的代码

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

这样不是更容易吗? 例如在 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/[ userName ]获取您的 id (大号)

我的答案建立在 Joaomgcd 广泛接受的答案之上。 如果用户安装了 Facebook 但是禁用了(例如通过使用 AppQuarantine) ,则此方法将不起作用。Twitter 应用程序的意图将被选中,但它将无法处理它,因为它是禁用的。

而不是:

context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));

您可以使用以下方法来决定应该做什么:

PackageInfo info = context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
if(info.applicationInfo.enabled)
return new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/620681997952698"));
else
return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/620681997952698"));

你可以点击以下按钮打开 facebook 应用程序:-

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {


startNewActivity("com.facebook.katana");
}
});


}


public void startNewActivity( String packageName)
{
Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null)
{
// we found the activity
// now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
// bring user to the market
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+packageName));
startActivity(intent);
}
}

在 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) {
// http://stackoverflow.com/a/24547437/1048340
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}

试试这个代码:

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

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 ID:

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进行了测试

经过多次测试,我找到了最有效的解决方案之一:

private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";


try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;


if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}

更可重用的方法

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

(类似于其他对事实的回答,把它放在这里只是为了简化和使实现可重用)

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

此方法将返回正确的网址应用程序如果安装或 web 网址如果应用程序没有安装。

然后开始一个意图如下:

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

这就够了。

下面是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);

我已经创建了一个方法来打开 facebook 页面到 facebook 应用程序,如果应用程序不存在,然后打开在铬

    String socailLink="https://www.facebook.com/kfc";
Intent intent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink);
if (facebookUrl == null || facebookUrl.length() == 0) {
Log.d("facebook Url", " is coming as " + facebookUrl);
return;
}
intent.setData(Uri.parse(facebookUrl));
startActivity(intent);

Class 添加这些方法

public static String getFacebookUrl(FragmentActivity activity, String facebook_url) {
if (activity == null || activity.isFinishing()) return null;


PackageManager packageManager = activity.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;
if (versionCode >= 3002850) { //newer versions of fb app
Log.d("facebook api", "new");
return "fb://facewebmodal/f?href=" + facebook_url;
} else { //older versions of fb app
Log.d("facebook api", "old");
return "fb://page/" + splitUrl(activity, facebook_url);
}
} catch (PackageManager.NameNotFoundException e) {
Log.d("facebook api", "exception");
return facebook_url; //normal web url
}
}

还有这个

 /***
* this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part
* @param context contain context
* @param url contains facebook url like https://www.facebook.com/kfc
* @return if it successfully split then return "kfc"
*
* if exception in splitting then return "https://www.facebook.com/kfc"
*
*/
public static String splitUrl(Context context, String url) {
if (context == null) return null;
Log.d("Split string: ", url + " ");
try {
String splittedUrl[] = url.split(".com/");
Log.d("Split string: ", splittedUrl[1] + " ");
return splittedUrl.length == 2 ? splittedUrl[1] : url;
} catch (Exception ex) {
return url;
}
}
Intent intent = null;
try {
getPackageManager().getPackageInfo("com.facebook.katana", 0);
String url = "https://www.facebook.com/"+idFacebook;
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href="+url));
} catch (Exception e) {
// no Facebook app, revert to browser
String url = "https://facebook.com/"+idFacebook;
intent = new Intent(Intent.ACTION_VIEW);
intent .setData(Uri.parse(url));
}
this.startActivity(intent);

这是我找到的最好的答案,效果很好。

只需在浏览器中进入你的 Facebook 页面,右击,然后点击“查看源代码”,然后找到 page_id属性: 你必须在这一行的最后一个反斜杠后面使用 page_id:

fb://page/pageID

例如:

Intent facebookAppIntent;
try {
facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/1883727135173361"));
startActivity(facebookAppIntent);
} catch (ActivityNotFoundException e) {
facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/CryOut-RadioTv-1883727135173361"));
startActivity(facebookAppIntent);
}

要做到这一点,我们需要“ Facebook 页面 ID”,你可以得到它:

  • 从页面转到“关于”。
  • 去“更多信息”部分。

introducir la descripción de la imagen aquí

要在指定的个人资料页面打开 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)));
}
try {
String[] parts = url.split("//www.facebook.com/profile.php?id=");
getPackageManager().getPackageInfo("com.facebook.katana", 0);
startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim()))));
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}

2018年7月开始,不管有没有 Facebook 应用程序,所有设备上都可以很好地工作。

private void goToFacebook() {
try {
String facebookUrl = getFacebookPageURL();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}


private String getFacebookPageURL() {
String FACEBOOK_URL = "https://www.facebook.com/Yourpage-1548219792xxxxxx/";
String facebookurl = null;


try {
PackageManager packageManager = getPackageManager();


if (packageManager != null) {
Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");


if (activated != null) {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;


if (versionCode >= 3002850) {
facebookurl = "fb://page/1548219792xxxxxx";
}
} else {
facebookurl = FACEBOOK_URL;
}
} else {
facebookurl = FACEBOOK_URL;
}
} catch (Exception e) {
facebookurl = FACEBOOK_URL;
}
return facebookurl;
}

要从应用程序启动 脸书主页,让 urlString = “ fb://page/your _ fb _ page _ id”

要启动 Facebook Messenger,让 urlString = “ fb-message://user/your _ fb _ page _ id”

FB 页 ID 通常是数字的。要得到它,到 找到我的联邦调查局证件输入您的个人资料网址,像 Www.facebook.com/edgedevstudio的东西,然后点击“查找数字 ID”。

现在,您已经得到了 fb 数字 ID。用生成的 Numeric ID 替换“ your _ fb _ page _ ID”

 val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
startActivity(intent)

2018年10月回答这个问题。工作代码是使用 pageID 的代码。我刚刚测试过,它的功能。

public static void openUrl(Context ctx, String url){
Uri uri = Uri.parse(url);
if (url.contains(("facebook"))){
try {
ApplicationInfo applicationInfo = ctx.getPackageManager().getApplicationInfo("com.facebook.katana", 0);
if (applicationInfo.enabled) {
uri = Uri.parse("fb://page/<page_id>");
openURI(ctx, uri);
return;
}
} catch (PackageManager.NameNotFoundException ignored) {
openURI(ctx, uri);
return;
}
}

我在 webview 中使用片段内部 oncreate 实现了这种形式:

 webView.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView viewx, String urlx)
{
if(Uri.parse(urlx).getHost().endsWith("facebook.com")) {
{
goToFacebook();
}
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
viewx.getContext().startActivity(intent);
return true;
}


});

以及 onCreateView:

 private void goToFacebook() {
try {
String facebookUrl = getFacebookPageURL();
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
} catch (Exception e) {
e.printStackTrace();
}
}


//facebook url load
private String getFacebookPageURL() {
String FACEBOOK_URL = "https://www.facebook.com/pg/XXpagenameXX/";
String facebookurl = null;


try {
PackageManager packageManager = getActivity().getPackageManager();


if (packageManager != null) {
Intent activated = packageManager.getLaunchIntentForPackage("com.facebook.katana");


if (activated != null) {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;


if (versionCode >= 3002850) {
facebookurl = "fb://page/XXXXXXpage_id";
}
} else {
facebookurl = FACEBOOK_URL;
}
} else {
facebookurl = FACEBOOK_URL;
}
} catch (Exception e) {
facebookurl = FACEBOOK_URL;
}
return facebookurl;
}

不使用 facebook sdk 打开按钮上的 fb 点击事件

 Intent FBIntent = new Intent(Intent.ACTION_SEND);
FBIntent.setType("text/plain");
FBIntent.setPackage("com.facebook.katana");
FBIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
try {
context.startActivity(FBIntent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(context, "Facebook have not been installed.", Toast.LENGTH_SHORT).show( );
}
fun getOpenFacebookIntent(context: Context, url: String) {
return try {
context.packageManager.getPackageInfo("com.facebook.katana", 0)
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/$url/")))
} catch (e: Exception) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}
}

1-得到您的 ID 去您的图像配置文件,并点击右键,并采取复制链接地址。

        try {
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("fb://profile/id"));
startActivity(intent);
} catch(Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("facebook url")));
}
}
});

声明常量

  private String FACEBOOK_URL="https://www.facebook.com/approids";
private String FACEBOOK_PAGE_ID="approids";

声明方法

public String getFacebookPageURL(Context context) {
PackageManager packageManager = context.getPackageManager();
try {
int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode;


boolean activated =  packageManager.getApplicationInfo("com.facebook.katana", 0).enabled;
if(activated){
if ((versionCode >= 3002850)) {
Log.d("main","fb first url");
return "fb://facewebmodal/f?href=" + FACEBOOK_URL;
} else {
return "fb://page/" + FACEBOOK_PAGE_ID;
}
}else{
return FACEBOOK_URL;
}
} catch (PackageManager.NameNotFoundException e) {
return FACEBOOK_URL;
}
}

调用函数

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

二零二零年三月开始,这个工作非常完美。

private void openFacebookPage(String pageId) {
String pageUrl = "https://www.facebook.com/" + pageId;


try {
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo("com.facebook.katana", 0);


if (applicationInfo.enabled) {
int versionCode = getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;
String url;


if (versionCode >= 3002850) {
url = "fb://facewebmodal/f?href=" + pageUrl;
} else {
url = "fb://page/" + pageId;
}


startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
} else {
throw new Exception("Facebook is disabled");
}
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(pageUrl)));
}
}

看看我打开 Facebook 特定页面的代码:

//ID initialization
ImageView facebook = findViewById(R.id.facebookID);


facebook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


String facebookId = "fb://page/327031464582675";
String urlPage = "http://www.facebook.com/MDSaziburRahmanBD";


try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(facebookId)));
}catch (Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(urlPage)));
}
}
});

你可以检查所有的 Facebook 应用程序是否安装了 Facebook 应用程序。 为了支持 OS 级别11,我们需要在 AndrodiManifest.xml中添加这个,以避免包名未找到异常-

<manifest ...
<queries>
<package android:name="com.facebook.katana" />
<package android:name="com.facebook.lite" />
<package android:name="com.facebook.android" />
<package android:name="com.example.facebook" />
</queries>
<application .....

然后将此方法添加到您的代码-

public static String isFacebookAppInstalled(Context context){


if(context!=null) {
PackageManager pm=context.getPackageManager();
ApplicationInfo applicationInfo;


//First check that if the main app of facebook is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
return applicationInfo.enabled?"com.facebook.katana":"";
} catch (Exception ignored) {
}


//Then check that if the facebook lite is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
return applicationInfo.enabled?"com.facebook.lite":"";
} catch (Exception ignored) {
}


//Then check the other facebook app using different package name is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
return applicationInfo.enabled?"com.facebook.android":"";
} catch (Exception ignored) {
}


try {
applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
return applicationInfo.enabled?"com.example.facebook":"";
} catch (Exception ignored) {
}
}
return "";
}

然后启动应用程序-

if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
/* Facebook App is installed,So launch it.
It will return you installed facebook app's package
name which will be useful to launch the app */


Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
if (intent != null) {
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
else {
Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intentForOtherApp);
}
}

答案对我很有用,但缺少的一件事是在清单中添加查询,以下是我的解决方案:

private void openFacebookApp() {
String facebookUrl = "www.facebook.com/XXXXXXXXXX";
String facebookID = "XXXXXXXXX";


try {
int versionCode = getActivity().getApplicationContext().getPackageManager().getPackageInfo("com.facebook.katana", 0).versionCode;


if(!facebookID.isEmpty()) {
// open the Facebook app using facebookID (fb://profile/facebookID or fb://page/facebookID)
Uri uri = Uri.parse("fb://page/" + facebookID);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else if (versionCode >= 3002850 && !facebookUrl.isEmpty()) {
// open Facebook app using facebook url
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
} else {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
} catch (PackageManager.NameNotFoundException e) {
// Facebook is not installed. Open the browser
Uri uri = Uri.parse(facebookUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}

}

并将其添加到应用程序标记外部的 Manifest 中

    <queries>
<package android:name="com.google.android.gm" />
<package android:name="com.facebook.katana" />
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.DIAL" />
<data android:scheme="tel" />
</intent>
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent>
</queries>

要从 Webview 打开 Facebook 应用程序中的 Facebook 页面,请在 should OverrideUrlLoading 方法中添加此代码

 if (url.startsWith("https://www.facebook.com || Your Facebook page address")) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=" + url)));
} catch (Exception e2) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
return true;
}