获得用户活动?

时间:2014-05-18 14:12:34

标签: android facebook facebook-graph-api android-fragments

我正在尝试使用FB Android sdk获取用户事件(One邀请他),但我不知道如何操作,我在他们的网站上找不到任何说明。

这是我同时的代码 -

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;

public class SocigoFragmentEntrance extends Fragment {

private final static String TAG  = "FB_LOGIN";

private UiLifecycleHelper uiHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActivity().getActionBar().hide();

    Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            onSessionStateChange(session, state, exception);
            Request.newMeRequest(session, new Request.GraphUserCallback() {

                @Override
                public void onCompleted(GraphUser user, Response response) {

                }
            });
        }
    };

     uiHelper = new UiLifecycleHelper(getActivity(), callback);
     uiHelper.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent
        , Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_entrance, parent, false);



    LoginButton fbButton = (LoginButton)v.findViewById(R.id.authButton);

    fbButton.setApplicationId(getResources().getString(R.string.app_id));
    fbButton.setReadPermissions(new String[]{"user_events", "user_interests", "user_likes"});
    fbButton.setFragment(this);

    return v;
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
}


@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onResume() {
    super.onResume();

    Session session = Session.getActiveSession();
    if (session != null &&
           (session.isOpened() || session.isClosed()) ) {
        onSessionStateChange(session, session.getState(), null);
    }

    uiHelper.onResume();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}


@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

}

1 个答案:

答案 0 :(得分:0)

我已经成功地研究了一下如何做到这一点。

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;

public class FragmentEntrance extends Fragment {

    private final static String TAG = "";

    private UiLifecycleHelper uiHelper;
    private String fqlQuery;

    //This for some reason has to be a variable
    private Session.StatusCallback callback = new Session.StatusCallback() {
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            onSessionStateChange(session, state, exception);
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActivity().getActionBar().hide();
        this.setRetainInstance(true);

            //Reset the query
        fqlQuery = "Empty";

        uiHelper = new UiLifecycleHelper(getActivity(), callback);
        uiHelper.onCreate(savedInstanceState);



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_entrance, parent, false);

        LoginButton fbButton = (LoginButton) v.findViewById(R.id.authButton);

            //Get permissions
        fbButton.setApplicationId(getResources().getString(R.string.app_id));
        fbButton.setReadPermissions(new String[] { "user_events",
                "user_interests", "user_likes" });
        fbButton.setFragment(this);

        return v;
    }

    private void onSessionStateChange(final Session session,
            SessionState state, Exception exception) {
        if (state.isOpened()) {
            Log.i(TAG, "User logged in.");
//Here we get the user's ID and use it with the query
            if (state != SessionState.OPENED) {
                Request.newMeRequest(session, new Request.GraphUserCallback() {

                    // callback after Graph API response with user object
                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if (user != null) {
                            fqlQuery = "SELECT eid FROM event_member WHERE start_time >= now() AND uid = '"
                                    + user.getId() + "'";
                            Bundle params = new Bundle();
                            params.putString("q", fqlQuery);

                            Log.d(TAG, fqlQuery);
                            Request request = new Request(session, "/fql",
                                    params, HttpMethod.GET,
                                    new Request.Callback() {
                                        public void onCompleted(
                                                Response response) {
                                            if (response.getError() == null) {
                                                //do things with the data
                                            } else {
                                                Toast.makeText(
                                                        getActivity(),
                                                        "Loading events failed.",
                                                        Toast.LENGTH_LONG)
                                                        .show();
                                            }

                                        }
                                    });
                            Request.executeBatchAsync(request);

                        }
                    }
                }).executeAsync();
            }


        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();

        Session session = Session.getActiveSession();
        if (session != null && (session.isOpened() || session.isClosed())) {
            onSessionStateChange(session, session.getState(), null);
        }

        uiHelper.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }



    }