【问题标题】:Open a facebook page from android app?从 android 应用程序打开一个 facebook 页面?
【发布时间】:2012-04-24 14:14:31
【问题描述】:

如何启动在手机上打开 Facebook 应用程序并导航到 Facebook 中首选页面的意图?

我试过了:

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

嗯,无论我写什么到“1234567891”,它总是导航到我的页面。总是对我而不是其他人。

我该怎么做?

【问题讨论】:

    标签: android android-intent facebook-android-sdk


    【解决方案1】:

    这是最好和最简单的方法。 跟着代码就行了

    public final void Facebook() {
            final String urlFb = "fb://page/"+yourpageid;
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(urlFb));
    
            // If Facebook application is installed, use that else 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/pages/"+pageid;
                intent.setData(Uri.parse(urlBrowser));
            }
    
            startActivity(intent);
        }
    

    【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题,发送了用户 ID,但由于某种原因,我的个人资料总是打开而不是朋友的个人资料。

    问题在于,如果您传递代表 Facebook UID 的 Long 对象的 String,甚至是 long 原始类型,则意图稍后将无法读取它。你需要传递一个真实的Long。

    所以完整的代码是:

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

    好享受,希望这会有所帮助:-)

    格言

    【讨论】:

    • 此解决方案将不再有效。您可以在以下链接stackoverflow.com/a/13107858/1020530中找到答案
    • 嘿,这不是一个好的解决方案,因为它明确使用 ProfileTabHostActivity。在未来的版本中,Facebook 可能会决定重命名该活动或将其从应用程序中删除。那么你的解决方案就行不通了。
    【解决方案3】:

    试试这个代码:

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

    【讨论】:

      【解决方案4】:

      此解决方案将不再有效。新版本的 facebook 应用程序不再支持这些意图。请参阅here 错误报告

      新的解决方案是使用iPhone方案机制(是的,facebook决定在Android中支持iPhone机制,而不是Android的隐式intent机制)。

      因此,要使用用户个人资料打开 facebook 应用程序,您只需:

      String facebookScheme = "fb://profile/" + facebookId;
      Intent facebookIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(facebookScheme)); 
      startActivity(facebookIntent);
      

      如果您正在寻找其他操作,您可以将following page 用于所有可用操作(/但您必须对其进行测试,因为我没有找到关于此的 facebook 官方出版物)

      【讨论】:

        猜你喜欢
        • 2011-06-16
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        相关资源
        最近更新 更多