会话管理有问题

Having trouble in Session management

我正在开发一个应用程序,我只想在 he/she 注销他之前的会话时让用户登录,但我无法做到这一点。每当我关闭我的程序并重新打开它时。即使我没有注销,它也会再次指向主页。请

  package in.co.medimap.www.medimap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static in.co.medimap.www.medimap.R.layout.login;

/**
 * Created by sony on 28-04-2016.
 */
public class login extends Activity {
TextView signup_text;
Button login_button;
EditText PHONE_NO,PASSWORD;
AlertDialog.Builder builder;
public static final String MyPREFERENCE ="Myprefs";
public static final String PHONE="phone";
public static final String PASS="password";
SharedPreferences sharedPreferences;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);



    setContentView(R.layout.login);
    signup_text=(TextView)findViewById(R.id.sign_up);
    signup_text.setOnClickListener(new View.OnClickListener(){

                                       @Override
                                       public void onClick(View v) {
                                           startActivity(new Intent(login.this,register.class));
                                       }
                                   });
    PHONE_NO=(EditText)findViewById(R.id.phone_no);
    PASSWORD=(EditText)findViewById(R.id.password);
    login_button=(Button)findViewById(R.id.login_button);

    login_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             String ph = PHONE_NO.getText().toString();
            String p=PASSWORD.getText().toString();


            if (ph.equals("")||p.equals("")) {

                builder = new AlertDialog.Builder(login.this);
                builder.setTitle("Something went wrong...");
                builder.setMessage("Please fill all the fields...");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();


                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
            else
            {
  //see from here
                sharedPreferences = getSharedPreferences(MyPREFERENCE,Context.MODE_PRIVATE );
                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString(PHONE, ph);
                editor.putString(PASS, p);
                editor.commit();

                BackgroundTask backgroundTask = new BackgroundTask(login.this);
                backgroundTask.execute("login",ph,p);

            }







}
});
    }
} 

这是我的家庭活动,登录后用户会去哪里 我希望如果我没有注销那么每当我打开我的应用程序时它应该从这里开始

package in.co.medimap.www.medimap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class HomeActivity extends Activity {

Button Logout = null;

TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Logout = (Button) findViewById(R.id.Logout);
    textView = (TextView) findViewById(R.id.welcome_txt);
    String message = getIntent().getStringExtra("message");
    textView.setText(message);

    Logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            editor.commit();
            Intent intent = new Intent(HomeActivity.this,MainActivity.class);
            startActivity(intent);



        }
    });




}
}

如果您希望每次用户离开应用程序时都注销,为什么不手动执行呢?

您已经设置了注销按钮 setOnClickListener 所以只需这样做:

@Override
protected void onPause() {
    super.onPause();
    if(Logout != null) {
        Logout.callOnClick();
    }
}

这意味着每次用户离开此 activity 都会注销。
确保你处理了这样的情况,如果他们离开 activity 但转到你页面上的另一个 activity,它不会注销。

=========== 编辑 ===========

我猜你的 login activity 是应用程序打开时启动的主要 activity,所以在执行任何其他任务之前将其放入 onCreate。在 setContentView 之后,你应该这样做。

SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);

String phone = sharedPreferences.getString(PHONE, "");
String pass = sharedPreferences.getString(PASS, "");

if(!phone.isEmpty() && !pass.isEmpty()) {
    // this means ID and passwords are already saved so just start your home activity here
    startActivity(new Intent(context, MainActivity.class));
    finish();
}

在你的MainActivity中:

只需读取 SharedPreferences 中的值并在需要时登录。

希望对您有所帮助。

========== 编辑 2

此外,您的注销应该如下所示。不要使用 commmitclear。输入空值并使用 apply

Logout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        SharedPreferences sharedPreferences = getSharedPreferences(login.MyPREFERENCE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(PHONE, "");
        editor.putString(PASS, "");
        editor.apply();
        Intent intent = new Intent(HomeActivity.this,MainActivity.class);
        startActivity(intent);
    }
});