2012-04-24 43 views
4

我该如何开始意图在手机上打开Facebook应用程序并导航到Facebook中的首选页面?从android应用程序打开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”,它总是浏览到我的网页。总是对我而不是其他人。

我怎么能这样做?

回答

4

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

问题是,如果您通过代表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); 

好享受,并希望这有助于:-)

马克西姆

+0

该解决方案将不再工作。你可以在以下链接找到答案http://stackoverflow.com/a/13107858/1020530 – nheimann1 2012-11-01 09:32:38

+0

嘿,这不是一个好的解决方案,因为它明确使用ProfileTabHostActivity。在未来的版本中,Facebook可能会决定重命名该活动或将其从应用中删除。那么你的解决方案将无法工作。 – Tomasz 2013-11-22 23:54:34

0

该解决方案将不再工作。新版Facebook应用不再支持这些意图。请参阅here错误报告

新的解决方案是使用iPhone方案机制(是的,Facebook决定支持Android中的iPhone机制而不是Android的隐含意图机制)。

因此,为了与用户打开Facebook的应用程序配置文件,所有你需要做的是:

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

如果您正在寻找其他操作就可以使用following page所有可用操作(/你有以测试它,因为我没有找到关于此的facebook的官方出版物)

+0

没有为我工作,因为我使用4.2.1安卓。 – 2014-08-13 13:38:33

11

这是最好的和简单的方法来做到这一点。 只需按照代码

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

这是最佳解决方案 – 2013-07-02 15:30:38

+0

谢谢。这工作。 – 2015-02-08 10:28:51

+0

它的工作,但在新的Facebook更新页面的链接是“https://www.facebook.com/"+pageid;这而不是“https://www.facebook.com/pages/"+pageid; – 2015-02-23 05:08:43

1

试试这个代码:

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))); 
    } 
相关问题