如何在Android上实现Facebook自动登录(Facebook SDK 4.0.0)?

时间:2017-06-25 12:22:44

标签: android facebook login

我使用Android Studio和Facebook SDK 4.0.0。目前我能够成功登录。但是,当我重新启动应用程序时,我必须再次登录。如何启动自动登录以便应用程序在启动时自动连接我的Facebook帐户?我已经尝试了会议'上课,但由于它已被“LoginManager”取代而无法工作。

2 个答案:

答案 0 :(得分:0)

根据facebook开发者'网站:

  

Android和iOS SDK目前不支持重新验证。

访问以下链接以了解详细信息: https://developers.facebook.com/docs/facebook-login/reauthentication

答案 1 :(得分:0)

首先使用sharedPreference获取用户名id,如下所示:

private  SharedPreferences settings ;
    private void graphRequest(AccessToken accessToken) {
        settings = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = settings.edit();
        GraphRequest request = GraphRequest.newMeRequest(accessToken,
        new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse   
                response) {
                    try {
                        String email = "";
                        if (object.has("email")) {
                            email = object.getString("email");
                        }
                        String id = object.getString("id");
                        String name = object.getString("name");
                        String gender = object.getString("gender");

                        editor.putString("username", id);
                        editor.putString("name", name);
                        editor.putString("gender", gender);
                        editor.putString("email", email);
                        editor.apply();

                        startActivity(new Intent(mContext, 
                        MainActivity.class));
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
         Bundle parameters = new Bundle();
         parameters.putString("fields", "id,name,email,gender");
         request.setParameters(parameters);
         request.executeAsync();
     }

检查条件OnResume(),如下所示:

@Override
protected void onResume() {
    settings =PreferenceManager.getDefaultSharedPreferences(this);
    if (!TextUtils.isEmpty(settings.getString("username", ""))) {
        startActivity(new Intent(mContext, MainActivity.class));
        finish();
      }
    Profile profile = Profile.getCurrentProfile();
    super.onResume();
}