我正在将我的应用程序从本地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.");
}
}
};
}
我在另一篇文章中找到了这个解决方案...
您也可以在Activity和Fragment中使用以下代码。什么时候 在Fragment中使用确保在ShareDialog构造函数中传递它。 如果你传递getActivity()那么onActivityResult方法将不会 在片段中触发。
我尝试将“mFragment.getActivity()”更改为“this”和“FacebookSocialSelector.this”,但我得到了一个无法解决的构造函数错误。
编辑:如果我使用shareDialog(mFragment)或shareDialog(fragment),程序编译正常,但我仍然没有收到回调。我尝试添加调试日志,但没有被触发。
答案 0 :(得分:2)
我怀疑mCallbackManager
没有调用onActivityResult()
因此您在注册的ShareDialog回调中没有收到任何结果。首先尝试调用new ShareDialog(mFragment);
而不是new ShareDialog(mFragment.getActivity());
然后,在您的ShareFragment类(我假设为mFragment
)中覆盖onActivityResult(requestCode, resultCode, data)
并执行以下操作:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mFacebookSocialSelector.onActivityResult(requestCode, resultCode, data);
}
mFacebookSocialSelector
指向您的FacebookSocialSelector类的实例。在你的FacebookSocialSelector类中创建一个公共方法,如下所示:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
希望onSuccess
一旦发布您的帖子就会被调用。