Creating a class for Facebook SDK 3.0 on Android

笑着哭i 提交于 2019-12-18 09:50:49

问题


Is it possible to create a class to encapsulate the Android SDK on android?

i.e - I don't want to clutter my activity with all the FB sessions and stuff, I want to have a class that I can call and it will do the job.

For example,

FacebookSDK sdk = new FacebookSDK();
sdk.publish();

For now I couldn't find a way to do this, without using Fragments or cluttering my activity with the facebook code.

Many Thank!


回答1:


In short, no.

Your activity needs to be notifying the Facebook SDK of lifecycle events in order to properly store state and handle the UI flow to and from the Facebook SDK activities.

UILifecycleHelper is an effort to encapsulate this as much as possible.

You may be able to unclutter your code by having all your activities extend some base class (i.e. FBActivity) that makes all the proper calls to UILifecycleHelper, though this only helps if all your activities are interacting with Facebook in a fairly uniform manner.




回答2:


Its all about session management.

//Check for active session -
Session session = Session.getActiveSession();

// if it doesnt exist create one-
if(session ==null)
session= new Session(getApplicationContext) 

// if it is not open open it first

    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this)
        .setPermissions(Arrays.asList("basic_info"))
        .setCallback(statusCallback));
    }
    else
    {
        Session.openActiveSession(getActivity(), this, true, statusCallback);
    }

    // callback listner fires when session state changes
    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(Session session, SessionState state, Exception exception) {
            //If your session is opened
            if(session.isOpened()
              publishData(session);
        } 

Method to publish your request -

    public void publishData(Session session)
    {
     OpenRequest open = new OpenRequest(this);
            open.setLoginBehavior(SessionLoginBehavior.SUPPRESS_SSO);
            open.setPermissions(Arrays.asList(new String[]{"email", "publish_actions", "user_birthday", "user_hometown"}));
            open.setCallback(this);
                    session.openForPublish(open);
    }

publishData defination can also be -

public void publishData(Session session)
        {
Bundle params = new Bundle();

params.putByteArray("message", "some message".egtBytes());
params.putByteArray("name", "some name".getBytes());
params.putByteArray("link", "some link".getBytes());
params.putByteArray("description", "Some description".getBytes());
params.putByteArray("picture", "picture.url".getBytes());

Request postToWall = Request.newRestRequest(session, 
                                                    "/" + pickedUsersId.get(0) + "/feed", params, HttpMethod.POST);
postToWall.setCallback( new Request.Callback() 
{

    @Override
    public void onCompleted(Response response) 
    {
         // get response
        Log.e(TAG, response.toString());

    }
});
Request.executeBatchAsync(postToWall);
}


来源:https://stackoverflow.com/questions/16814195/creating-a-class-for-facebook-sdk-3-0-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!