2011-10-17 171 views
7

我想在我的android应用程序中显示像按钮。以下是我用于在Android应用程序中显示Like按钮的代码。Facebook Like按钮在Android应用程序

String url = "http://www.facebook.com/plugins/like.php?layout=standard&show_faces=true&width=80&height=50&action=like&colorscheme=light&href=http://beta.demo.dy/web/service/304.htm" 
webview = (WebView) findViewById(R.id.webView1); 
webview.loadUrl(url); 
webview.setWebViewClient(new LikeWebviewClient()); 

public class LikeWebviewClient extends WebViewClient { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 
} 

但是当我运行这个应用程序时,它显示一个白色区域。如何解决这个问题?

+0

嘿,我想要做同样的事情。请告诉我它解决了吗?如果是的话,你是怎么做到的? –

+0

可以添加,所述网页视图设置onlu像按钮被显示给用户的webview.so的高度和宽度。 – Megha

回答

4

Facebook SDK尚未提供用于本机移动应用程序的like按钮功能(截至2011年10月17日)。它在移动Web应用程序中可用。 欲了解更多信息,您可以检查这些链接:
Mobile - Facebook Developers
Like Button - Facebook Developers

+0

我想显示android.it的Webview中可能在ios中他们也disaply webview的像按钮,所以我想那 – Megha

+1

使用库在这篇文章中描述:http://stackoverflow.com/a/23853937/1891118 –

0

试试这个我做了一些修改

String url = "http://www.facebook.com/plugins/like.php?layout=standard&show_faces=true&width=80&height=50&action=like&colorscheme=light&href=http://beta.demo.dy/web/service/304.htm"; 
       // webview = (WebView) findViewById(R.id.webview); 
        webView.loadUrl(url); 
        webView.setWebViewClient(new WebViewClient()); 
       class LikeWebviewClient extends WebViewClient 
        { 
         public boolean shouldOverrideUrlLoading(WebView view, String url) { 
          view.loadUrl(url); 
          return true; 
         } 
        } 
6

等等选项可以在本地平台的语言很容易实现(安卓, iOS)以及浏览器(curl,PHP,JavaScript)如下。 转到developer.facebook应用程序部分并在Developer App配置的Open Graph部分中添加内置的Like操作,该操作在添加新的Open Graph操作时应显示在下拉列表中。请参阅here了解最新更新。

完成后,请选择“获取代码”选项来检索Android平台的示例代码,如下所示。你可以选择你选择的平台。请注意,app_specific_unique_identifier对于应用程序是唯一的,我出于安全原因已将其删除,您必须将其用于您的应用程序。

我已经能够成功测试Like流。希望这可以帮助。

Bundle params = new Bundle(); 
params.putString("object", "http://samples.ogp.me/<app_specific_unique_identifier>"); 

Request request = new Request(
    Session.getActiveSession(), 
    "me/og.likes", 
    params, 
    HttpMethod.POST 
); 
Response response = request.executeAndWait(); 
// handle the response 
+0

这是一个很好的答案。我一直在寻找这样一个连续三天的答案。非常感谢! – overbet13

+0

@birender你能帮我解决这个问题吗?谢谢。 –

+0

我没有内置(普通)类似动作的选项。有什么建议么? –

3

最后的Facebook,并推出Like按键为Android

步骤:

1 - 添加Facebook Library到项目

2 - Create App在Facebook上 3 - 更新清单

**In the Application tab add meta-data** 

<meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/fb_id" /> 

4 - 添加LikeView在布局

//activitymain.xml 
<com.facebook.widget.LikeView 
      android:id="@+id/like_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      > 
     </com.facebook.widget.LikeView> 

5 - ActivityMain.java

//set facebook page or link to this like button 
LikeView likeView; 
UiLifecycleHelper uiHelper; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activitymain); 
    uiHelper = new UiLifecycleHelper(this, null); 
    likeView = (LikeView) findViewById(R.id.like_view); 
    likeView.setObjectId("https://www.facebook.com/<page_username>");//it can be any link 

    likeView.setLikeViewStyle(LikeView.Style.STANDARD); 
    likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE); 
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.LEFT); 

} 

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    uiHelper.onActivityResult(requestCode, resultCode, data, null); 

} 

输出 enter image description here

+0

有没有一种方法可以像应用程序本身一样?就像Google Play中的Google plus one按钮一样。 –

1

您可以在Android应用使用特殊权限从Facebook开发者帐户https://developers.facebook.com使用Facebook像按钮。 在此处添加您的应用并提交应用特别许可。 转到应用程序审查并提交项目以供审批。 点击开始提交和选择LIKE本地按钮后,并提交他们会喜欢你为什么要得到这样的许可,您的应用程序将如何使用此权限的一切的所有细节。 如果Facebook将批准你的要求,那么你可以使用Facebook喜欢里面的应用程序按钮。 enter code here

 <com.facebook.share.widget.LikeView 
     android:id="@+id/facebooklike" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     </com.facebook.share.widget.LikeView> 

之后你需要做一些java代码。

likeView = (LikeView) findViewById(R.id.facebooklike); 
    likeView.setLikeViewStyle(LikeView.Style.STANDARD);    
    likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE); 
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.CENTER); 
    likeView.setObjectIdAndType("url of like page", LikeView.ObjectType.PAGE); 

一样的功能,当你点击按钮一样自动调用。

现在你需要得到像页面,用户像不像页面,页面响应。

enter code here 

公共类FbLikes扩展AppCompatActivity {

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fb_likes); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     if (resultCode == RESULT_OK) { 
      // verify we're returning from like action 
       // get action results 
       bundle = data.getExtras().getBundle("com.facebook.platform.protocol.RESULT_ARGS"); 
       if (bundle != null) { 
        like = bundle.getBoolean("object_is_liked");// liked/unliked 
        bundle.getInt("didComplete"); 
        bundle.getInt("like_count"); // object like count 
        bundle.getString("like_count_string"); 
        bundle.getString("social_sentence"); 
        bundle.getString("completionGesture"); // liked/cancel/unliked 
        Log.e(TAG, bundle.getString("social_sentence") + ""); 
        Log.e(TAG, "likeornot" + bundle.getBoolean("object_is_liked") + ""); 
        Log.e(TAG, "lcomplete" + bundle.getString("completionGesture") + ""); 
        Log.e(TAG, "count" + bundle.getInt("like_count") + ""); 
        Log.e(TAG, "countstr" + bundle.getString("like_count_string") + ""); 
        Log.e(TAG, "did" + bundle.getInt("didComplete") + ""); 
       } 
     } 
    } catch (Exception e) { 

    } 
} 

} 这个代码将返回你喜欢的功能想要的一切。