在我的安卓应用中,我想在官方的Facebook应用中打开一个指向Facebook个人资料的链接(当然,如果该应用已经安装)。对于iPhone来说,存在fb://
的URL方案,但在我的Android设备上尝试同样的事情时,会出现ActivityNotFoundException
。
是否有机会从代码中打开Facebook官方应用程序中的Facebook个人资料?
这在最新版本上是可行的。
1.转到https://graph.facebook.com/
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应用程序不再支持这种方式,有另一种方式,请看下面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/pages/"+pageid;
intent.setData(Uri.parse(urlBrowser));
}
startActivity(intent);
}
这已经被FrAndroid论坛上的Pierre87逆向工程,但我找不到任何官方描述它的地方,所以它必须被视为无文档的,并有可能在任何时候停止工作。
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);