在会话管理方面遇到麻烦

时间:2016-05-08 12:37:31

标签: java android session sharedpreferences session-management

我正在处理一个应用程序,我想只在他/她注销他以前的会话时才进行用户登录,但我无法做到。每当我关闭我的程序并重新打开它。即使我没有注销,它也会再次指向主页。 PLZ

  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);



        }
    });




}
}

1 个答案:

答案 0 :(得分:1)

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

您已经设置了退出按钮setOnClickListener,所以只需执行此操作:

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

这意味着每次用户离开此活动时都会退出 确保你处理的情况是,如果他们离开活动但是在你的页面上进行不同的活动,它就不会退出。

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

我猜你的login活动是应用程序打开时启动的主要活动,所以在执行任何其他任务之前将其放在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);
    }
});