我正在将我的应用程序从本地Share Api转换为ShareDialog。对话框正确显示,但是我的应用程序未收到回叫信息,表明帖子已完成/失败/取消。我正在片段中使用此ShareDialog,我相信这可能是我的问题的一部分。为什么没有收到回叫?
这是代码...
public class FacebookSocialSelector extends SocialSelector {
private static final String FACEBOOK_EMAIL_PREFERENCE = "facebook_email";
private final CallbackManager mCallbackManager;
private String mFbEmail;
ShareDialog shareDialog;
public FacebookSocialSelector(ShareFragment fragment, View v, ImageView iconView) {
super(fragment, v, iconView);
// Social login
FacebookSdk.sdkInitialize(fragment.getContext().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(mCallbackManager,
mFacebookLoginCallback);
shareDialog = new ShareDialog(mFragment.getActivity());
setBackgroundDisabledDrawable(R.drawable.sharebtn_left_disable);
setBackgroundEnabledDrawable(R.drawable.sharebtn_left_enable);
setDisabledDrawable(R.drawable.facebook_disable);
setEnabledDrawable(R.drawable.facebook_enable);
}
@Override
public void login() {
// Check for existing login
final String fbEmail;
if (mFbEmail == null) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(mFragment.getActivity());
fbEmail = prefs.getString(FACEBOOK_EMAIL_PREFERENCE, null);
} else {
fbEmail = mFbEmail;
}
if (AccessToken.getCurrentAccessToken() != null && fbEmail != null) {
if (Profile.getCurrentProfile() != null) {
finishLogin(true);
mFragment.fbInfo(Profile.getCurrentProfile().getId(),
Profile.getCurrentProfile().getName(), fbEmail);
} else {
new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
finishLogin(true);
mFragment.fbInfo(currentProfile.getId(),
currentProfile.getName(), fbEmail);
Profile.setCurrentProfile(currentProfile);
stopTracking();
}
};
Profile.fetchProfileForCurrentAccessToken();
}
return;
}
LoginManager.getInstance().logInWithReadPermissions(mFragment.getActivity(),
Arrays.asList("public_profile", "email"));
}
public void finishLogin(boolean success) {
if (success) {
select();
mFragment.loginSuccess();
} else {
mFragment.loginFailed("Could not login to FaceBook");
}
}
public void doPost(String message, Bitmap bitmap) {
ShareDialog shareDialog = new ShareDialog(mFragment.getActivity());
if (ShareDialog.canShow(SharePhotoContent.class)) {
Bitmap image = bitmap;
SharePhoto photo = new SharePhoto.Builder()
.setBitmap(image)
// Hashtag Still not working for some reason
// .setShareHashtag(new ShareHashtag.Builder()
// .setHashtag("#ConnectTheWorld")
// .build());
.build();
SharePhotoContent content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
shareDialog.show(content);
shareDialog.registerCallback(mCallbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
mFragment.postFinish(true);
}
@Override
public void onCancel() {
mFragment.postFinish(false);
}
@Override
public void onError(FacebookException error) {
mFragment.postFinish(false);
}
});
}
}
@Override
public void onLoginCallback(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPostCallback(int requestCode, int resultCode, Intent data) {
}
private FacebookCallback<LoginResult> mFacebookLoginCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Iterator<String> iterator =
loginResult.getRecentlyGrantedPermissions().iterator();
boolean hasPublishPermissions = false;
while (iterator.hasNext()) {
String s = iterator.next();
if (s.equalsIgnoreCase("publish_actions")) {
hasPublishPermissions = true;
}
}
if (!hasPublishPermissions) {
LoginManager.getInstance().logInWithPublishPermissions(mFragment.getActivity(),
Arrays.asList("publish_actions"));
} else {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
mGraphCallback);
Bundle parameters = new Bundle();
parameters.putString("fields", "email");
request.setParameters(parameters);
request.executeAsync();
}
}
@Override
public void onCancel() {
Toast.makeText(mFragment.getActivity(), "Login Cancel",
Toast.LENGTH_LONG).show();
finishLogin(false);
LoginManager.getInstance().logOut();
AccessToken.setCurrentAccessToken(null);
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(mFragment.getActivity(), exception.getMessage(),
Toast.LENGTH_LONG).show();
finishLogin(false);
LoginManager.getInstance().logOut();
AccessToken.setCurrentAccessToken(null);
}
};
GraphRequest.GraphJSONObjectCallback mGraphCallback = new GraphRequest.GraphJSONObjectCallback() {
private ProfileTracker mProfileTracker;
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
mFbEmail = object.getString("email");
final Profile currentProfile = Profile.getCurrentProfile();
PreferenceManager
.getDefaultSharedPreferences(mFragment.getActivity())
.edit()
.putString(FACEBOOK_EMAIL_PREFERENCE, mFbEmail)
.apply();
mFragment.loginSuccess();
if (currentProfile == null) {
// Profile not yet loaded
mFragment.fbInfo(null, null, mFbEmail);
mProfileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
finishLogin(true);
mFragment.fbInfo(currentProfile.getId(),
currentProfile.getName(), mFbEmail);
Profile.setCurrentProfile(currentProfile);
stopTracking();
}
};
} else {
mFragment.fbInfo(currentProfile.getId(),
currentProfile.getName(), mFbEmail);
}
} catch (JSONException ex) {
mFragment.loginFailed("Could not log in to FaceBook.");
}
}
};
}
我在另一篇文章中找到了这个解决方案...
您也可以在“活动”和“片段”中使用以下代码。什么时候
在Fragment中使用,请确保在ShareDialog构造函数中传递它。
如果您通过getActivity(),则onActivityResult方法将不会
在片段中触发。
并且我尝试将“ mFragment.getActivity()”更改为“ this”和“ FacebookSocialSelector.this”,但遇到了无法解决的构造函数错误。
编辑:如果我使用shareDialog(mFragment)或shareDialog(fragment),程序可以正常编译,但是我仍然没有收到回调。我尝试添加调试日志记录,但是没有一个被触发。
最佳答案
我怀疑没有调用mCallbackManager
,因此您在注册到ShareDialog的回调中不会收到任何结果。首先尝试调用onActivityResult()
而不是new ShareDialog(mFragment);
然后,在您的ShareFragment类(我假设是new ShareDialog(mFragment.getActivity());
)中覆盖mFragment
并执行以下操作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebookSocialSelector.onActivityResult(requestCode, resultCode, data);
}
其中
onActivityResult(requestCode, resultCode, data)
指向您的FacebookSocialSelector类的实例。在您的FacebookSocialSelector类中,创建一个公共方法,如下所示:public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
希望一旦发布您的帖子,就会呼叫
mFacebookSocialSelector
。