重启android应用程序后如何在fb sdk中保持登录状态

时间:2016-07-20 17:20:28

标签: android facebook facebook-graph-api

我正在android中登录facebook登录页面并在登录时获取用户详细信息但是当我关闭应用程序并重新启动它时,我的所有用户详细信息都消失了,登录活动再次启动它,但是fb要求退出。我的问题是我想要的保持登录不亮我退出。我的代码是

      FacebookCallback<LoginResult> mCallBack=new 
FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {


    // Facebook Email address
   GraphRequest request = GraphRequest.newMeRequest(
            loginResult.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {

                public void onCompleted(
                        JSONObject object,
                        GraphResponse response) {
                    Log.v("LoginActivity Response ", response.toString());

                  try {


                        Name = object.getString("name");
                      Email = object.getString("email");
                     // Gender=object.getString("gender");
                      BD=object.getString("link");




           profilePicUrl =   
     object.getJSONObject("picture").getJSONObject("data").getString("url");

            //    tv1.setText(profilePicUrl);
          shared prefernce
                      SharedPreferences sharedPref = 
    PreferenceManager.getDefaultSharedPreferences(userLogin.this);
                      //now get Editor
                      SharedPreferences.Editor editor = sharedPref.edit();
                      //put your value
                      editor.putString("name1",Name);
                      editor.putString("mail2", Email);
                      editor.putString("picture", profilePicUrl);
                      //commits your edits
                      editor.commit();

                      Intent returnIntent = new 
    Intent(userLogin.this,tabLayout.class);
                      returnIntent.putExtra("name1",Name);
                      returnIntent.putExtra("mail2", Email);
                      returnIntent.putExtra("picture", profilePicUrl);

                      startActivity(returnIntent);




                      //    finish();

                    }
                    catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });


    Bundle parameters = new Bundle();

    parameters.putString("fields", 
   "id,name,link,email,picture.type(large)");
    request.setParameters(parameters);
    request.executeAsync();




}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException error) {

}
};
    loginButton.registerCallback(callbackManager,mCallBack);

}

////

2 个答案:

答案 0 :(得分:0)

检查是否

r=R+1

if(AccessToken.getCurrentAccessToken() == null){// AccessToken is a class of fb sdk
     // try to login with facebook
     //show facebook login
}else{
  // get your data from Shared preferences
  // move to next activity
}

答案 1 :(得分:0)

LoginManager.getInstance()。registerCallback(callbackManager,new FacebookCallback(){                     @覆盖                     public void onSuccess(LoginResult loginResult){

                    if (AccessToken.getCurrentAccessToken() != null) {
                        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(final JSONObject object, GraphResponse response) {

                                        try {


                                            //add your name/email/profile picture url parsing and sharedPreference code here


                                        } catch (JSONException e) {
                                            Toast.makeText(context, "" + "Unable to log In", Toast.LENGTH_SHORT).show();
                                            e.printStackTrace();
                                        }
                                    }

                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,link,email,picture.type(large)");
                        request.setParameters(parameters);
                        request.executeAsync();
                    } else {
                    }

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException error) {

                }
            }

    );

//以这种方式编码,然后它应该工作。实际上,Facebook SDK会在您登录后将会话保留在内存中,因此在您从帐户中卸载或删除应用程序之前,它不会被注销。希望它有所帮助。