我的应用程序有分享按钮来分享用户时间轴上的故事。我正在做的是,用户点击共享按钮,如果用户会话不存在应用程序强制使用 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:我不需要单独的登录按钮。我想要的是,用户点击分享按钮,如果登录它需要发布故事,否则登录用户,然后直接发布故事。