gpt4 book ai didi

java - Android Facebook SDK ShareDialog 不返回回调

转载 作者:行者123 更新时间:2023-11-29 19:12:44 27 4
gpt4 key购买 nike

我正在将我的应用程序从 native Share Api 转换为 ShareDialog。对话框显示正确,但我的应用程序没有收到说明帖子已完成/失败/取消的回调。我在一个 fragment 中使用这个 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.");
}
}
};

}

我在另一篇文章中找到了这个解决方案...

You can use the code below in Activity and in Fragment as well. When using in Fragment make sure you pass this in ShareDialog constructor. If you pass getActivity() then onActivityResult method will not be triggered in Fragment.

并且我已经尝试将“mFragment.getActivity()”更改为“this”和“FacebookSocialSelector.this”,但我得到了一个 connot resolve constructor 错误。

编辑:如果我使用 shareDialog(mFragment) 或 shareDialog(fragment),程序可以正常编译,但我仍然没有收到回调。我尝试添加调试日志记录,但没有被触发。

最佳答案

我怀疑 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)方法,如下所示:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}

希望 onSuccess 会在您发布帖子后被调用。

关于java - Android Facebook SDK ShareDialog 不返回回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44720533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com