2014-08-31 57 views
0

我不明白哪里出错 我基本上是这样做的,当我点击按钮“fb”时,应用程序通过包含链接在Facebook上打开一个共享,但只要我测试,它就会打开Facebook没有链接的页面? 你能帮助我吗? 我想知道你是否也可以输入一个固定的文本和链接(如果可能的话,你可能会看到我的代码行)? 感谢如何创建Android意图在Facebook上分享?

这是代码

final Button button = (Button) findViewById(R.id.fb); 
button.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 

     String urlToShare = "https://www.google.it/?gfe_rd=cr&ei=IysDVMyHPMjD8gesvYH4DA"; 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("text/plain"); 
     // intent.putExtra(Intent.EXTRA_SUBJECT, "Foo bar"); // NB: has no effect! 
     intent.putExtra(Intent.EXTRA_TEXT, urlToShare); 

     // See if official Facebook app is found 
     boolean facebookAppFound = false; 
     List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent, 0); 
     for (ResolveInfo info : matches) { 
      if (info.activityInfo.packageName.toLowerCase().startsWith("com.facebook.katana")) { 
       intent.setPackage(info.activityInfo.packageName); 
       facebookAppFound = true; 
       break; 
      } 
     } 

     // As fallback, launch sharer.php in a browser 
     if (!facebookAppFound) { 
      String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + urlToShare; 
      intent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl)); 
     } 

     startActivity(intent); 

回答

-1

这下面的代码可以共享所有的社交网络:

 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
     sharingIntent.setType("text/plain"); 
     sharingIntent.putExtra(Intent.EXTRA_TEXT,urlToShare); 
     startActivity(Intent.createChooser(sharingIntent,"Şhare")); 
+0

谢谢,但只有facebook?我想立即开始facebook – User777 2014-08-31 14:27:15

+0

http://stackoverflow.com/questions/3515198/share-text-on-facebook-from-android-app-via-action-send – ferandro 2014-08-31 14:33:06

0
The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields. 

这是错误链接:https://developers.facebook.com/bugs/332619626816423

事情是,如果你把一个URL放在EXTRA_TEXT字段中,它可以工作。这就像他们故意剥去任何文字

编辑: 最新的Facebook版本不允许您使用意图共享文本。您必须使用Facebook SDK才能做到这一点 - 为此,请使用Facebook SDK + Android Simple Facebook(https://github.com/sromku/android-simple-facebook)。

+0

好的谢谢,所以我该怎么办? :) – User777 2014-08-31 14:27:56

+0

检查我的编辑...也看到这个链接http://stackoverflow.com/questions/22533773/android-how-to-share-image-with-text-on-facebook-via-intent – Jamil 2014-08-31 14:32:54