我按照facebook文档在我的应用程序中集成了facebook登录,我也从图形API请求中获取了电子邮件ID。但我希望当没有电子邮件ID时,登录受限制...... 我的LoginFrament是
public class LoginFragment extends Fragment implements View.OnClickListener {
private View mView;
private TextInputLayout til_emailid, til_password;
private EditText et_emailid, et_password;
Boolean eye_open = false;
LoginButton btn_facebooklogin;
private Button btn_login;
Context mContext;
AccessToken accessToken;
AccessTokenTracker accessTokenTracker;
ProfileTracker profileTracker;
private CallbackManager mcallbackManager;
String username, password, fbemail;
public LoginFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mcallbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_login, container, false);
btn_facebooklogin = (LoginButton) mView.findViewById(R.id.btn_facebooklogin);
btn_facebooklogin.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
btn_facebooklogin.setFragment(this);
btn_facebooklogin.registerCallback(mcallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
loginResult.getAccessToken();
if (Profile.getCurrentProfile() == null) {
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
Log.v("facebook - profile", profile2.getName());
profileTracker.stopTracking();
profile.setCurrentProfile(profile2);
}
};
} else {
Profile profile = Profile.getCurrentProfile();
Log.v("facebook - profile", profile.getFirstName());
}
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try {
String email = object.getString("email");
String birthday = object.getString("birthday");
String name = object.getString("name");
String gender = object.getString("gender");
Utils.showToast(mContext, name+email+birthday+gender);
} catch (JSONException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
});
return mView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mcallbackManager.onActivityResult(requestCode, resultCode, data)) {
return;
} else {
Utils.showToast(mContext, "NO Results from Activity");
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login:
Login();
break;
}
}
在这里我需要facebook电子邮件,如果它不在那里,那么登录不得发生...... ???
答案 0 :(得分:0)
如果电子邮件为空或空,请使用以下代码从Facebook注销
LoginManager.getInstance().logOut();