从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个回答

7
截至 2020 年 3 月 ,此项工作完美运行。
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)));
    }
}

我正在寻找最近的答案,谢谢!另外,我查了一下那个版本,其中“"fb://page/"”已被弃用,而且它是从2014年的版本。因此,我认为不必包含早期访问链接的方法是安全的。 - Matt Strom

5
经过多次测试,我找到了一种非常有效的解决方案:
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));
    }
}

4

我找到的最佳答案,效果非常好。

只需在浏览器中打开 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);
}

这只对静态应用程序有意义。 - Irshu

4

我的回答基于joaomgcd广泛接受的答案。如果用户已经安装了Facebook但被禁用(例如使用App Quarantine),这种方法将不起作用。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"));

3
你可以检查所有Facebook应用程序是否已安装。 为了支持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);
                } 
 }

不幸的是,通过katana调用它对我没用,它总是打开我的个人资料页面,而不是指定的页面。 - Itapox
1
@Itapox 我正在研究这个主题,因为我也遇到了同样的问题。完成后,我会编辑这个问题。谢谢。 - Gk Mohammad Emon

2
截至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;
}

FB Lite应用程序具有单独的包名称,如果您使用此方式,则还应检查com.facebook.lite - 尽管我尚未检查Lite是否会处理这些URL。 - androidguy
@androidguy,我没有FB Lite。知道代码是否与其兼容会很好。如果您能以某种方式测试它并在此处发布您的发现,那将有助于其他仍在使用此代码的人。感激不尽,谢谢。 - Immy
@Immy --> 在URL中的Yourpage-1548219792xxxxxx是页面ID或页面URL。例如:https://www.facebook.com/PageName 是我的页面URL,而ID类似于2345676。因此,在代码中提到的URL将是https://www.facebook.com/PageName或其他什么? - Neeraj Dwivedi
@NeerajDwivedi 你需要在URL中同时包含页面名称和页面ID。类似这样:'String FACEBOOK_URL = "https://www.facebook.com/BeanRoaster-1548219792112345/";' - Immy
@Immy 我已经使用完全相同的代码,将FACEBOOK_URL替换为“https://www.facebook.com/myPageName-myPageID/”,但我得到了一个带有顶部搜索栏的空白页面。我正在使用Android 9上的最新Facebook应用程序(Fb应用程序版本-237.0.0.44.120)。 - Neeraj Dwivedi
@NeerajDwivedi 我刚在一台安卓9三星手机上尝试了一下,它可以正常工作。请再次检查页面名称和ID。我知道这听起来很傻,但我想不到其他的原因了。或者,尝试从代码中复制您的URL并将其粘贴到浏览器的地址栏中,以查看是否有效。 - Immy

2

要从您的应用程序启动Facebook页面,请使用urlString =“fb://page / your_fb_page_id”

要启动Facebook Messenger,请使用urlString =“fb-messenger://user / your_fb_page_id”

FB页面ID通常是数字。 要获取它,请转到查找我的FB ID,输入您的个人资料网址,例如www.facebook.com/edgedevstudio,然后单击“查找数字ID”。

哇,现在您有了自己的fb数字ID。 用生成的数字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)

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

2

查看我打开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)));
            }
        }
    });

2

声明常量

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

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