public class NextActivity extends AppCompatActivity {
CallbackManager callbackManager;
LoginManager manager;
ImageView image;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Button shareButton = (Button)findViewById(R.id.sharebtn);
image = (ImageView) findViewById(R.id.image);
FacebookSdk.sdkInitialize(getApplicationContext());
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkpermission();
}});
}
public void checkpermission(){
final Bundle permBundle = new Bundle();
permBundle.putCharSequence("permission", "publish_actions");
GraphRequest request = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions", permBundle, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
System.out.println("sammy_permission_graph_request "+graphResponse.getJSONObject());
try {
JSONArray permList = (JSONArray) graphResponse.getJSONObject().get("data");
if(permList.length() == 0){
// no data for perms, hence asking permission
getPublishPermission();
}else{
JSONObject permData = (JSONObject) permList.get(0);
String permVal = (String) permData.get("status");
if(permVal.equals("granted")){
sharePhotoToFacebook();
}else{
getPublishPermission();
}
}
} catch (JSONException e) {
System.out.println("sammy_exception while parsing fb check perm data "+e.toString());
}
}
}
);
request.executeAsync();
}
public void getPublishPermission(){
callbackManager = CallbackManager.Factory.create();
manager = LoginManager.getInstance();
manager.logInWithPublishPermissions(
NextActivity.this,
Arrays.asList("publish_actions"));
manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("sammy_got_publish_permission");
sharePhotoToFacebook();
}
@Override
public void onCancel() {
System.out.println("sammy_cancelled while asking publish permission");
}
@Override
public void onError(FacebookException error) {
System.out.println("sammy_error occurred while asking publish permission!");
}
});
}
private void sharePhotoToFacebook(){
System.out.println("sammy_sharing_started");
try{
Bitmap bm = ((BitmapDrawable)image.getDrawable()).getBitmap();
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(bm)
.setCaption("BMW Vision")
.setUserGenerated(true)
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
ShareApi.share(content, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result)
{
System.out.println("sammy_publish_result "+result);
Toast.makeText(NextActivity.this, "Success in Posting", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel()
{
System.out.println("sammy_shareapi_canceled");
}
@Override
public void onError(FacebookException e)
{
System.out.println("sammy_shareapi_exception "+e);
}
});
}catch (Exception e){
System.out.println("sammy_exception "+e.toString());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
我正在尝试在后台分享图片和文字到Facebook。首先,我会检查是否已经授予发布权限。如果没有,我会使用logInWithPublishPermissions
来获取发布权限。但是由于某些原因,注册回调的响应总是在onCancel()
中。我需要在logInWithPublishPermissions
中添加什么吗?