从我的Android应用程序中,我想要在官方Facebook应用程序中打开一个链接到Facebook个人资料的链接(如果已经安装该应用程序)。对于iPhone设备,存在fb://
URL方案,但是在我的Android设备上尝试相同的事情会抛出ActivityNotFoundException
异常。
有没有可能从代码中打开官方Facebook应用程序的个人资料页面?
从我的Android应用程序中,我想要在官方Facebook应用程序中打开一个链接到Facebook个人资料的链接(如果已经安装该应用程序)。对于iPhone设备,存在fb://
URL方案,但是在我的Android设备上尝试相同的事情会抛出ActivityNotFoundException
异常。
有没有可能从代码中打开官方Facebook应用程序的个人资料页面?
这个方法适用于最新版本:
使用此方法:
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。在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) {
// https://dev59.com/62445IYBdhLWcg3wia2V#24547437
uri = Uri.parse("fb://facewebmodal/f?href=" + url);
}
} catch (PackageManager.NameNotFoundException ignored) {
}
return new Intent(Intent.ACTION_VIEW, uri);
}
这样不是更容易吗?比如在一个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/[用户名]获取您的id(大数字)
对于 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个人资料:
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进行了测试。
以下是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);
这是做这件事最简单的代码。
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);
}
一种更可重用的方法。
这是我们在大多数应用程序中通常使用的功能。因此,这里提供了一个可重用的代码片段来实现它。
(和其他答案类似,只是为了简化并使实现可重用而发布)
"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
}
}
如果已安装应用程序,则此方法将返回正确的应用程序URL;如果未安装应用程序,则返回Web URL。
然后按以下方式启动意图:
Intent facebookIntent = new Intent(Intent.ACTION_VIEW);
String facebookUrl = getFacebookPageURL(this);
facebookIntent.setData(Uri.parse(facebookUrl));
startActivity(facebookIntent);
这就是你所需要的。
为了完成这个任务,我们需要获取“Facebook页面ID”,您可以通过以下步骤获得:
您可以执行以下操作:
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)));
}
curl -skA“Mozilla/5.0”https://www.facebook.com/mypage/ | grep -oE“fb://[^\"]+”
- ubuntudroid这个被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);
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)));
}