从Android应用打开Facebook页面?

从我的Android应用程序,我想打开Facebook官方Facebook应用程序(如果应用程序安装,当然)的Facebook配置文件的链接。 对于iPhone,存在fb:// URL方案,但在我的Android设备上尝试同样的事情会抛出ActivityNotFoundException异常。

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

这适用于最新版本:

  1. 转到https://graph.facebook.com/ < user_name_here >(例如https://graph.facebook.com/fsintents
  2. 复制你的ID
  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>")); } } 

这将打开Facebook应用程序,如果用户已经安装。 否则,它会在浏览器中打开Facebook。

编辑:从版本11.0.0.11.23(3002850)Facebook应用程序不再支持这种方式,还有另一种方法,检查从Jare​​d Rummler下面的答复。

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

这不容易吗? 例如在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"))); } 

PS。 从http://graph.facebook.com/%5BuserName%5D获取你的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进行了测试

这是做这个最简单的代码

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

这是2016年的方式,很好,很容易。

我查看了Facebook发送的电子邮件是如何打开应用程序的。

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

更可重用的方法。

这是我们大多数应用程序通常使用的功能。 因此,这是一个可重用的代码片段来实现这一点。

(类似于事实的其他答案,在这里只是为了简化和使实现可重用)

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

这就是你所需要的。

这是在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); 

试试这个代码:

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

我的答案建立在joaomgcd广为接受的答案之上。 如果用户安装了Facebook但禁用了(例如使用应用程序隔离),则此方法将不起作用。 该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")); 

经过多次测试,我找到了最有效的解决方案之一:

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

最佳答案我发现,它工作得很好。

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

要做到这一点,我们需要“Facebook页面ID”,你可以得到它:

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

介绍了描述和图片

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

你可以这样做:

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

"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页面到Facebook应用程序,如果应用程序不存在,然后打开铬

  String socailLink="https://www.facebook.com/kfc"; Intent intent = new Intent(Intent.ACTION_VIEW); String facebookUrl = Utils.getFacebookUrl(getActivity(), socailLink); if (facebookUrl == null || facebookUrl.length() == 0) { Log.d("facebook Url", " is coming as " + facebookUrl); return; } intent.setData(Uri.parse(facebookUrl)); startActivity(intent); 

Utils.class添加这些方法

 public static String getFacebookUrl(FragmentActivity activity, String facebook_url) { if (activity == null || activity.isFinishing()) return null; PackageManager packageManager = activity.getPackageManager(); try { int versionCode = packageManager.getPackageInfo("com.facebook.katana", 0).versionCode; if (versionCode >= 3002850) { //newer versions of fb app Log.d("facebook api", "new"); return "fb://facewebmodal/f?href=" + facebook_url; } else { //older versions of fb app Log.d("facebook api", "old"); return "fb://page/" + splitUrl(activity, facebook_url); } } catch (PackageManager.NameNotFoundException e) { Log.d("facebook api", "exception"); return facebook_url; //normal web url } } 

和这个

  /*** * this method used to get the facebook profile name only , this method split domain into two part index 0 contains https://www.facebook.com and index 1 contains after / part * @param context contain context * @param url contains facebook url like https://www.facebook.com/kfc * @return if it successfully split then return "kfc" * * if exception in splitting then return "https://www.facebook.com/kfc" * */ public static String splitUrl(Context context, String url) { if (context == null) return null; Log.d("Split string: ", url + " "); try { String splittedUrl[] = url.split(".com/"); Log.d("Split string: ", splittedUrl[1] + " "); return splittedUrl.length == 2 ? splittedUrl[1] : url; } catch (Exception ex) { return url; } } 
 try { String[] parts = url.split("//www.facebook.com/profile.php?id="); getPackageManager().getPackageInfo("com.facebook.katana", 0); startActivity(new Intent (Intent.ACTION_VIEW, Uri.parse(String.format("fb://page/%s", parts[1].trim())))); } catch (Exception e) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } 

您可以按下按钮,打开Facebook应用程序,如下所示: –

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startNewActivity("com.facebook.katana"); } }); } public void startNewActivity( String packageName) { Intent intent = MainActivity.this.getPackageManager().getLaunchIntentForPackage(packageName); if (intent != null) { // we found the activity // now start the activity intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { // bring user to the market // or let them choose an app? intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id="+packageName)); startActivity(intent); } }