6

我的应用程序有分享按钮来分享用户时间轴上的故事。我正在做的是,用户点击共享按钮,如果用户会话不存在应用程序强制使用 Facebook 登录用户。当用户这样做时,故事需要直接发布。这是我的代码。

onCreate 方法

    isShared = false;
    uiHelper = new UiLifecycleHelper(this, statusCallback);
    uiHelper.onCreate(savedInstanceState);
    Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
    Session session = Session.getActiveSession();
    if (session == null) {

        if (savedInstanceState != null) {
            session = Session.restoreSession(this, null, statusCallback,
                    savedInstanceState);
        }
        if (session == null) {
            session = new Session(this);
        }
        Session.setActiveSession(session);
        if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
            session.openForRead(new Session.OpenRequest(this)
                    .setCallback(statusCallback));
        }
    }       

    if (savedInstanceState != null) {
        pendingPublishReauthorization = savedInstanceState.getBoolean(
                PENDING_PUBLISH_KEY, false);
    }

    shareBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            doPostStory();                              
        }
    });

剩下的代码是 -

    private void doPostStory() {
    Session session = Session.getActiveSession();
    if (!session.isOpened() && !session.isClosed()) {
        session.openForRead(new Session.OpenRequest(this)
                .setCallback(statusCallback));
    } else {
        isShared = true;
        Session.openActiveSession(this, true, statusCallback);
        publishStory();         
    }
}

private class SessionStatusCallback implements Session.StatusCallback {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        if(!isShared){
            updateView();
            isShared = true;
        }
    }
}

private void updateView() {
    Session session = Session.getActiveSession();
    if (session.isOpened()) {
        if(!isShared){
            publishStory();
        }
        Log.i("Session State callback", "Logged in");
    } else {
        Log.i("Session State Callback", "Logged Out");                      
    }
}   

@Override
public void onStart() {
    super.onStart();
    Session.getActiveSession().addCallback(statusCallback);
}

@Override
public void onStop() {
    super.onStop();
    Session.getActiveSession().removeCallback(statusCallback);
}

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

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Session session = Session.getActiveSession();
    Session.saveSession(session, outState);
    outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
    uiHelper.onSaveInstanceState(outState);
}

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

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

publishStory() 的代码

    private void publishStory() {
    Session session = Session.getActiveSession();

    if (session != null) {

        // Check for publish permissions
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            pendingPublishReauthorization = true;
            Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
                    this, PERMISSIONS);
            session.requestNewPublishPermissions(newPermissionsRequest);
            return;
        }

        Bundle postParams = new Bundle();
        postParams.putString("name", "Some");
        postParams.putString("caption",updateTitle);
        postParams.putString("description",updateText);
        postParams.putString("link","http://somedomain.com");
        postParams.putString("picture","someurl");

        Request.Callback callback = new Request.Callback() {
            public void onCompleted(Response response) {
                JSONObject graphResponse = response.getGraphObject()
                        .getInnerJSONObject();
                String postId = null;
                try {
                    postId = graphResponse.getString("id");
                } catch (JSONException e) {
                    Log.i("asd", "JSON error " + e.getMessage());
                }
                FacebookRequestError error = response.getError();
                if (error != null) {
                    Toast.makeText(UpdateDetails.this,
                            error.getErrorMessage(), Toast.LENGTH_SHORT)
                            .show();
                } else {
                    Toast.makeText(UpdateDetails.this, postId,
                            Toast.LENGTH_LONG).show();
                }
            }
        };

        Request request = new Request(session, "me/feed", postParams,
                HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();
    } else {
        Log.i("Session Sate", "close");
    }

}

代码确实在用户时间线上发布了故事。但它多次发布。这意味着,statusCallback被解雇多次。我尝试设置标志来检查故事帖子的状态。但是还是发了好几遍。

任何想法这段代码有什么问题?高度赞赏帮助。

PS:我不需要单独的登录按钮。我想要的是,用户点击分享按钮,如果登录它需要发布故事,否则登录用户,然后直接发布故事。

4

2 回答 2

3

Create Facebook Session Object
Then Just Apply This in onCreate Method.

 Session.openActiveSession(this, true, new Session.StatusCallback() {

        // callback when session changes state
        @Override
        public void call(Session session, SessionState state,
                Exception exception) {
            if (session.isOpened()) {

                // make request to the /me API
                Request.newMeRequest(session,
                        new Request.GraphUserCallback() {

                            // callback after Graph API response with user
                            // object
                            @Override
                            public void onCompleted(GraphUser user,
                                    Response response) {
                                FacebookLoginActivity.this.progressDialog = ProgressDialog
                                        .show(YourActivity.this,
                                                "",
                                                "",
                                                true);
                                if (user != null) {

                                   Intent passIntent = new Intent(YourActivity.this,Second.class)
                                   startActivity(passIntent)
                                }
                            }
                        }).executeAsync();

            }
        }
    });
于 2014-10-14T08:42:03.123 回答
3

删除所有publishStory()调用。在你的回调中这样做:

public void call(Session session, SessionState state,
        Exception exception) {
    if(session != null && session.isOpened() && isShared){
       publishStory();
    }
}

在您的UpdateDetails班级中声明isSharedboolean isShared = false; 用户点击共享按钮时使其变为真,例如:

shareBtn.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        isShared = true;
        doPostStory();                              
    }
});

在你publishStory的 sonCompleteisShared作假。喜欢:

public void onCompleted(Response response) {
    isShared = false;
    ...................
    ...................
}

希望这会有所帮助。

于 2013-03-08T16:29:39.123 回答