Facebook 注销显示一些异常行为,Android

Facebook logout shows some unusual behaviour,Android

我正在尝试开发一个我正在使用 facebook 的应用程序 login.when 我从另一个 activity.it 使用 logoutfromfacebook 开始登录 activity 并突然自动使用以前的 information.i 登录我无法使用新的 loginId.I 登录,我不知道它是什么。要么我犯了一个严重的错误,要么犯了一个愚蠢的错误。 这是我的代码 -

public class LoginActivity extends  AbsRuntimePermission {
private static final int REQUEST_Permission =10;


    private TextView info;
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    private SparseIntArray mErrorString;
    private Button facebook_button;
    ProgressDialog progress;
    private String facebook_id,f_name,m_name,l_name,gender,full_name,email_id;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

      //facebook sdk
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    //for permission in manifest file cause it is launch activity
    requestAppPermissions(new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.INTERNET},R.string.msg,REQUEST_Permission);

    //facebook
    info = (TextView) findViewById(R.id.info);
    loginButton = (LoginButton) findViewById(R.id.login_button);
    progress = new ProgressDialog(LoginActivity.this);
    progress.setMessage(getResources().getString(R.string.please_wait_facebooklogin));
    progress.setIndeterminate(false);
    progress.setCancelable(true);

    facebook_id=f_name=m_name=l_name=gender=full_name=email_id="";

    LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "public_profile"));
    //loginButton.setReadPermissions("email", "public_profile");

    //register callback object for facebook result

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(final LoginResult loginResult) {


            progress.show();
            Profile profile = Profile.getCurrentProfile();
            Log.d("is null", String.valueOf(profile==null));
              if(profile != null) {
                facebook_id = profile.getId();
                f_name = profile.getFirstName();
                m_name = profile.getMiddleName();
                l_name = profile.getLastName();
                       //show Toast
                GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject object, GraphResponse response) {
                                try {
                                   String email_id = object.getString("email");
                                   // String gender = object.getString("gender");
                                    String Profile_name = object.getString("name");
                                    long fb_id = object.getLong("id"); //use this for logout

                                    //Shared preference

                                    sharedPreferences = getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
                                    editor = sharedPreferences.edit();
                                    editor.putString("User_Name", Profile_name);
                                    editor.putString("User_Email",email_id);
                                    editor.putString("f_id", String.valueOf(fb_id));
                                    editor.apply();
                                  //Starting a new activity using this information
                                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                                    i.putExtra("type", "facebook");
                                    i.putExtra("Facebook_id", fb_id);
                                    i.putExtra("full_name", Profile_name);
                                    i.putExtra("email_id", email_id);
                                    //i.putExtra("gender", gender);

                                    progress.dismiss();
                                    startActivity(i);
                                    finish();

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "email,id,name");
                request.setParameters(parameters);
                request.executeAsync();
            }
        }


        @Override
        public void onCancel() {
        Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_canceled_facebooklogin),Toast.LENGTH_SHORT).show();
        progress.dismiss();
        }

        @Override

        public void onError(FacebookException error) {
            Toast.makeText(LoginActivity.this,getResources().getString(R.string.login_failed_facebooklogin),Toast.LENGTH_SHORT).show();
            progress.dismiss();
        }
    });

    //facebook button click
    loginButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile","user_friends","email"));

        }
    });
}

 public void logoutFromFacebook(){
    try{
        if(AccessToken.getCurrentAccessToken()==null){
            return;
        }

        String fb1_id=sharedPreferences.getString("f_id","");
        GraphRequest graphRequest = new GraphRequest(AccessToken.getCurrentAccessToken(),"/"+fb1_id+"/permissions/",null,
                HttpMethod.DELETE,new GraphRequest.Callback(){
            @Override
            public void onCompleted(GraphResponse response) {
                PreferenceManager.getDefaultSharedPreferences(getBaseContext()).
                        edit().clear().apply();
                LoginManager.getInstance().logOut();

            }
        });
        graphRequest.executeAsync();
    }catch (Exception ex){
        ex.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
}

@Override
public void onPermissionGranted(int requestCode) {

    //anything after permission Granted
    Toast.makeText(getApplicationContext(),"Permission granted",Toast.LENGTH_LONG).show();

}



public void goTo(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    this.startActivity(intent);
}

如果您使用另一个 activity 进行注销,那么请这样做:

if (AccessToken.getCurrentAccessToken() == null) {

Toast.makeText(getApplicationContext(), "please Wait ", Toast.LENGTH_SHORT).show(); }

              new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/", null, HttpMethod.DELETE, new GraphRequest.Callback() {
                  @Override
                  public void onCompleted(GraphResponse graphResponse) {
                      LoginManager.getInstance().logOut();

                      Toast.makeText(getApplicationContext(), "Logout Successfully", Toast.LENGTH_LONG).show();
                      Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                      startActivity(intent);
                      finish();


                  }
              }).executeAsync();
          }