Android Facebook sdk 分享到用户墙上
Android Facebook sdk share on user wall
我有两个活动:第一个 activity 有一个 Facebook 登录按钮,用户可以在其中成功登录。在接下来的 activity 中有一个分享到用户墙上的按钮。现在在第一次分享之前,它会从用户那里获得发布权限。当用户允许时,它会返回,但不会继续共享操作。但是下次我去同一个 activity 并按下按钮分享,然后每次都分享。
所以问题是当第一次获得用户许可时它不会继续其共享操作。我也希望它第一次工作。用户同时允许这两个操作,然后立即进行共享。下面是我的代码:
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
{
// Check for publish permissions
if (!hasPublishPermission())
{
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
ringProgressDialog = ProgressDialog.show(context, "Please wait ...", "Sharing on facebook ...", true);
Bundle postParams = new Bundle();
postParams.putString("name", "Test");
postParams.putString("message", "Test message");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
try
{
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(getClass().getSimpleName(), "Facebook JSON error " + e.getMessage());
} catch (Exception e) {
Log.i(getClass().getSimpleName(), "Facebook error " + e.getMessage());
}
ringProgressDialog.dismiss();
FacebookRequestError error = response.getError();
if (error != null) {
Utils.showToast_msg(context, error.getErrorMessage());
} else {
Utils.showToast_msg(context, "Shared Successfully");
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
////////////////////////////////////////// //////////////////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
在此区块中:
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
最后,您使用的是return;
,所以即使您成功获得新权限,if
之后的代码也不会被调用。第二次成功是因为这个block没有执行,app已经有权限了
异步方式 (reference) 是:(某些函数名称可能已关闭)
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
newPermissionsRequest.setCallback(new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
publishStory();
}
});
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
我有两个活动:第一个 activity 有一个 Facebook 登录按钮,用户可以在其中成功登录。在接下来的 activity 中有一个分享到用户墙上的按钮。现在在第一次分享之前,它会从用户那里获得发布权限。当用户允许时,它会返回,但不会继续共享操作。但是下次我去同一个 activity 并按下按钮分享,然后每次都分享。
所以问题是当第一次获得用户许可时它不会继续其共享操作。我也希望它第一次工作。用户同时允许这两个操作,然后立即进行共享。下面是我的代码:
private void publishStory() {
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
{
// Check for publish permissions
if (!hasPublishPermission())
{
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
ringProgressDialog = ProgressDialog.show(context, "Please wait ...", "Sharing on facebook ...", true);
Bundle postParams = new Bundle();
postParams.putString("name", "Test");
postParams.putString("message", "Test message");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
try
{
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(getClass().getSimpleName(), "Facebook JSON error " + e.getMessage());
} catch (Exception e) {
Log.i(getClass().getSimpleName(), "Facebook error " + e.getMessage());
}
ringProgressDialog.dismiss();
FacebookRequestError error = response.getError();
if (error != null) {
Utils.showToast_msg(context, error.getErrorMessage());
} else {
Utils.showToast_msg(context, "Shared Successfully");
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
////////////////////////////////////////// //////////////////
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(Session.getActiveSession() != null)
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
在此区块中:
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
最后,您使用的是return;
,所以即使您成功获得新权限,if
之后的代码也不会被调用。第二次成功是因为这个block没有执行,app已经有权限了
异步方式 (reference) 是:(某些函数名称可能已关闭)
if (!hasPublishPermission()) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest((Activity)context, Arrays.asList("publish_actions"));
newPermissionsRequest.setCallback(new StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened() && session.getPermissions().contains("publish_actions")) {
publishStory();
}
});
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}