通过记住我复选框保存登录凭据(用户名和密码)

时间:2014-08-22 05:43:46

标签: android sharedpreferences android-checkbox

我是Android的新手,并且在我的应用程序中第一次使用“记住我”复选框来保存用户名和密码。我已经尝试了很多代码并且做了R& D在Google上使用此功能,但没有成功。在此应用程序中,用户名(studentId)是从服务器获取的密码。如果有人帮助我,我会感激不尽。谢谢

这是我的代码:

public class LoginActivityB extends Activity {

String studentID, password;
TextView errorTV;

private CheckBox saveLoginCheckBox;
private Boolean saveLogin,saveAuth;
LoginDataBaseAdapter dbase;
private Handler handler;
EditText editTextUserName, editTextRe_enterPassword;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_login_activity_b);
    ImageView btnSignIn = (ImageView) findViewById(R.id.buttonSignIn);
    loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
    loginPrefsEditor = loginPreferences.edit();
    saveLogin = loginPreferences.getBoolean("saveLogin", false);
    saveAuth = loginPreferences.getBoolean("saveAuth", false);

   dbase = new LoginDataBaseAdapter(LoginActivityB.this);
    editTextUserName = (EditText) findViewById(R.id.editID);
    editTextRe_enterPassword = (EditText) findViewById(R.id.editTextPassword);
    editTextRe_enterPassword.setVisibility(View.GONE);
    final EditText editTextPassword = (EditText) findViewById(R.id.editPassword);
    TextView forgotPasswordTV = (TextView) findViewById(R.id.forgotPasswordTV);
    saveLoginCheckBox = (CheckBox) findViewById(R.id.rememberMeCB);
    errorTV = (TextView) findViewById(R.id.errorTV);

    if (saveLogin == true) {
        saveLoginCheckBox.setChecked(true);
        Intent dashboard = new Intent(getApplicationContext(),
                DashboardActivity.class);
        startActivity(dashboard);
        finish();
    }

    btnSignIn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            studentID = editTextUserName.getText().toString();
            password = editTextPassword.getText().toString();
            String reEnterPassword = editTextRe_enterPassword.getText()
                    .toString();

            new AsyncCaller().execute();
            stopNewService();
            startNewService();
        }
    });

   public void startNewService() {
    startService(new Intent(this, com.education.service.MyService.class));
    }

public void stopNewService() {

    stopService(new Intent(this, com.education.service.MyService.class));
    }
public void connection() {
        final AsyncHttpClient myClient = new AsyncHttpClient();
        myClient.setTimeout(60000);
        myClient.addHeader("Content-type",
                "application/x-www-form-urlencoded");
        myClient.addHeader("X_MOBICOACH_KEY", "MOBICOACH2013");
        RequestParams params = new RequestParams();
        params.put("studentid", studentID);
        params.put("password", password);
        params.put("stream", "engg");

        myClient.post(Config.urlPath + "/SaveData", params,
                new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(String response) {
                        System.out.println(response);
                        if (response != null) {
                            if (response.contains("OK")) {
                                String userID = response.substring(3);
                                // String ID = removeLastChar(userID);

                                dbase.deleteAll();
                                dbase.insertEntry(studentID, password,
                                        userID);

                                if (saveLoginCheckBox.isChecked()) {

                                loginPrefsEditor.putBoolean("saveLogin", true);
                                loginPrefsEditor.putString("username", studentID);
                                loginPrefsEditor.putString("password", password);
                                loginPrefsEditor.putString("value", "1");
                                loginPrefsEditor.putString("userID", userID);
                                loginPrefsEditor.commit();

                                  } else { 
                                      loginPrefsEditor.clear();
                                      loginPrefsEditor.commit(); }


                                Intent dashboard = new Intent(
                                        getApplicationContext(),
                                        DashboardActivity.class);
                                startActivity(dashboard);
                                finish();

                            }
                        }
  }

1 个答案:

答案 0 :(得分:0)

最好在将值保存到共享首选项之前初始化loginPrefsEditor

SharedPreferences.Editor loginPrefsEditor= getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", studentID);
-----   
loginPrefsEditor.commit();

修改 根据评论中的要求:

if (saveLogin == true) 
{
   // The below lines should be removed.
   // saveLoginCheckBox.setChecked(true);
   // Intent dashboard = new Intent(getApplicationContext(),DashboardActivity.class);
   // startActivity(dashboard);
   // finish();

   // Instead, add the below lines:
   editTextUserName.setText (loginPreferences.getString("username", ""));
   editTextRe_enterPassword.setText (loginPreferences.getString("password", ""));

}

这确保了,如果选中“记住我”复选框,则会使用用户名和密码预先填充登录屏幕。用户只需点击登录按钮即可触发验证部分,按钮点击监听器逻辑将继续。