Creating a class for Facebook SDK 3.0 on Android

后端 未结 2 1777
我在风中等你
我在风中等你 2020-12-22 11:03

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

2条回答
  •  情书的邮戳
    2020-12-22 11:21

    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);
    }
    

提交回复
热议问题