如何在Android中使用Graph api实现Facebook Like功能

时间:2016-05-27 10:25:42

标签: android facebook facebook-graph-api facebook-sdk-4.0 android-remoteview

我需要在我的小部件应用程序中实现Facebook Like功能。为此,我试图从SDK compile 'com.facebook.android:facebook-android-sdk:4.0.1'实现Facebook LikeView。但不幸的是我无法使用此视图,因为小部件使用RemoteViews。所以我完全坚持使用Graph api来实现 Facebook赞的方法,因为缺乏此目的的文档。我发布了我用于Facebook的代码,如下所示

MainActivity.java

public class MainActivity extends Activity {


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

        LikeView likeView = (LikeView) findViewById(R.id.likeView);
        likeView.setLikeViewStyle(LikeView.Style.STANDARD);
        likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);

        likeView.setObjectIdAndType(
                "https://www.facebook.com/JavaDigest/",
                LikeView.ObjectType.OPEN_GRAPH);

    }

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.facebooklike.MainActivity">

    <com.facebook.share.widget.LikeView
        android:id="@+id/likeView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

的AndroidManifest.xml

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

    <provider
        android:name="com.facebook.FacebookContentProvider"
        android:authorities="com.facebook.app.FacebookContentProvider2XXXXXXXXXXX85"
        android:exported="true" />

任何对我有帮助的替代解决方案。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我还实现了类似Facebook的功能,并且我也成功地使用了LikeView.ObjectType.OPEN_GRAPH。

以下是详细说明

的代码

在MainActivity中定义

callbackManager = CallbackManager.Factory.create();
likeView = (LikeView) findViewById(R.id.likeview);

likeView.setLikeViewStyle(LikeView.Style.BOX_COUNT);
likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
likeView.setObjectIdAndType(
            "http://shareitexampleapp.parseapp.com/photo1/",
             LikeView.ObjectType.OPEN_GRAPH);

MainActivity onCreate方法

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     callbackManager.onActivityResult(requestCode, resultCode, data);
 }

在您的Mainactivity中添加onActivityResult方法

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

<强>的AndroidManifest.xml

//Remove this provider

<provider
     android:name="com.facebook.FacebookContentProvider"
     android:authorities="com.facebook.app.FacebookContentProvider2XXXXXXXXXXX85"
     android:exported="true" />

并从清单中删除提供商

{{1}}