从Android应用打开Facebook页面?

Bjö*_*lek 154 android facebook url-scheme

从我的Android应用程序,我想在官方Facebook应用程序中打开一个Facebook个人资料的链接(当然,如果安装了该应用程序).对于iPhone,存在fb://URL方案,但在我的Android设备上尝试相同的操作会引发一个问题ActivityNotFoundException.

有没有机会通过代码在官方Facebook应用程序中打开Facebook个人资料?

joa*_*gcd 235

这适用于最新版本:

  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>"));
       }
    }
    
    Run Code Online (Sandbox Code Playgroud)

如果用户安装了Facebook应用程序,这将打开它.否则,它将在浏览器中打开Facebook.

编辑:自版本11.0.0.11.23(3002850)Facebook应用程序不再支持这种方式,还有另一种方法,请查看以下来自Jared Rummler的回复.

  • 这是这个问题的答案; 使用"页面"而不是个人资料 - http://stackoverflow.com/a/15872383/1433187 (7认同)
  • 有关iOS上可用的网址方案列表,请参阅http://wiki.akosma.com/IPhone_URL_Schemes - 其中一些也适用于Android (3认同)
  • DjHacktorReborn是对的,fb:// profile/<fpid>和fb:// page/<fpid>都不再起作用了fb v11 (3认同)
  • 这是在Facebook论坛的任何地方记录的吗? (2认同)

Jar*_*ler 145

在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);
}
Run Code Online (Sandbox Code Playgroud)

  • @DjHacktorReborn你应该不再使用id.只需使用普通链接即可. (4认同)
  • 谁能确认这仍然有效?(截至2019年6月)。我仍然得到`ActivityNotFoundException` (3认同)
  • @DjHacktorReborn刚刚检查过,在这里工作.在Facebook版本13.0.0.13.14 (2认同)
  • 这会打开Facebook页面,但与常规的fb页面不同,我们看不到“赞”按钮,个人资料和封面照片等。它直接打开“联系信息”并显示该页面中的所有帖子。 (2认同)

Joh*_*ohn 38

这不容易吗?例如在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")));
}
Run Code Online (Sandbox Code Playgroud)

PS.从http://graph.facebook.com/ [userName]获取您的ID(大号)


Mur*_*mel 31

对于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));
}
Run Code Online (Sandbox Code Playgroud)

对于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));
}
Run Code Online (Sandbox Code Playgroud)

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

两者都在Facebook v.27.0.0.24.15和Nexus 4上的Android 5.0.1上进行了测试


Som*_*123 27

这是2016年的方法,效果很好,非常简单.

在查看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);
Run Code Online (Sandbox Code Playgroud)

  • 好的,但没有带我到一个干净的页面,用户可以轻松地使我的页面像....仍在寻找这个!有解决方案吗 (4认同)
  • 当我使用它时,我正在引用的Facebook页面的标题在Facebook应用程序中丢失,例如页面配置文件图像和页面/活动/见解选项卡. (3认同)

MBH*_*MBH 21

这是执行此操作的最简单的代码

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/pages/"+pageid;
            intent.setData(Uri.parse(urlBrowser));
        }

        startActivity(intent);
    }
Run Code Online (Sandbox Code Playgroud)


And*_*tel 15

一种更可重用的方法.

这是我们在大多数应用中通常使用的功能.因此,这是一个可重用的代码来实现这一目标.

(类似于事实方面的其他答案.在此发布它只是为了简化并使实现可重用)

"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
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果安装了app,此方法将返回正确的url url,如果未安装app,则返回web url.

然后按如下方式启动意图:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
Run Code Online (Sandbox Code Playgroud)

这就是你所需要的.


小智 11

"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
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果安装了app,此方法将返回正确的url url,如果未安装app,则返回web url.

然后按如下方式启动意图:

Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
Run Code Online (Sandbox Code Playgroud)


Jon*_*son 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);
Run Code Online (Sandbox Code Playgroud)


Rav*_*ana 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)));
        }
Run Code Online (Sandbox Code Playgroud)


Dev*_*cie 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)));
    }
}
Run Code Online (Sandbox Code Playgroud)


Jor*_*sys 6

为此,我们需要“ Facebook页面ID”,您可以获取它:

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

图像描述入门

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

你可以这样做:

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

或者,您可以验证未安装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(url)));
        }
Run Code Online (Sandbox Code Playgroud)


Jav*_*nas 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));
    }
}
Run Code Online (Sandbox Code Playgroud)