我想實現我的Android應用程序Facebook的登錄按鈕,這是我迄今所做的:Android的登錄按鈕沒有調用onActivityResult或回調方法
public class LoginFragment extends BaseFragment {
private View mView;
private CallbackManager mCallbackManager;
private LoginButton mFBLoginButton;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getActivity());
mCallbackManager = CallbackManager.Factory.create();
mView = inflater.inflate(R.layout.login, container, false);
mFBLoginButton = (LoginButton) mView.findViewById(R.id.login_facebook_button);
mFBLoginButton.setReadPermissions("public_profile", "user_friends","user_birthday","user_about_me","email");
mFBLoginButton.setFragment(this);
mFBLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("LoginFragment", "UserId: " + loginResult.getAccessToken().getUserId());
}
@Override
public void onCancel() {
Log.d("LoginFragment", "Operation Canceled");
}
@Override
public void onError(FacebookException e) {
e.printStackTrace();
}
});
return mView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getBaseActivity().displayToastMessage("Hey");
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
我還添加了Facebook活動和應用程序清單中的Id。該應用程序運行良好,當我點擊登錄按鈕,它將我帶到Facebook對話框。這裏的問題是沒有任何回調方法被執行,即onSuccess(),onCancel()和onError()。 onActivityResult()也不會被執行。請告訴我,如果我錯過了什麼
這解決了我的問題。謝了哥們 – saintjab