整数共享首选项始终返回0

时间:2018-09-07 16:22:36

标签: android passwords sharedpreferences

我正在尝试编写一个简单的密码检查代码,以保护我的应用程序设置。密码应以共享首选项保存。当sharedPreference返回零时,主调用setPassword。这是为了确保在有人尝试首次输入设置时设置密码。然后,setPassword应该设置一个密码并将其保存在sharedPreference中。但是,当检查是否设置了密码时,sharedPreference始终返回0。

这是我的主要爱好

final Intent intentSetPasswords = new Intent(this, SetPasswordActivity.class);
    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);

    //setting button on click listener
    buttonSetings.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //check if a password was set muss Überarbeitet werden
            passwordToCheck = sharedPreferences.getInt("PASSWORDSETTINGS", 0);
            Log.e("password", ""+ passwordToCheck);
            if(passwordToCheck==0){
                startActivity(intentSetPasswords);
            }else{
                startActivity(new Intent(MainActivity.this, PasswordCheckActivity.class));
            }
        }
    });

tis是我的setPassword:

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

    final Intent intentSettings = new Intent(this, SettingsActivity.class);


    buttonSetPassword = (Button)findViewById(R.id.buttonSetPassword);
    textViewSetPassword = (TextView)findViewById(R.id.textViewSetPasswordText);
    editTextPassword = (EditText)findViewById(R.id.passwordNumericSettingsSetPassword);
    final Intent intent = new Intent(this, MainActivity.class);
    buttonSetPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            counterButton++;
            switch (counterButton){
                case 1:
                    password = editTextPassword.getText().toString();
                    if (password.length() == 4){
                        Log.d("First Password", "taken");
                        buttonSetPassword.setText("Confirm Password!");
                        editTextPassword.setText("");
                    }else{
                        Log.d("First Password", "not taken");
                        textViewSetPassword.setText("The Password must be\nfour numbers long!");
                        password = "";
                        editTextPassword.setText("");
                        counterButton=0;
                    }
                    break;
                case 2:
                    passwordToConfirm = editTextPassword.getText().toString();
                    if (Integer.parseInt(passwordToConfirm)  == Integer.parseInt(password)){

                        Log.e("password", password);
                        Log.e("passwordToConfirm", passwordToConfirm);
                        SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putInt("PASSWORDSETTINGS", Integer.parseInt(password));
                        editor.commit();
                        startActivity(intentSettings);
                        finish();
                    }else{
                        textViewSetPassword.setText("The passwords aren't the same!\nTry again!");
                        buttonSetPassword.setText("Set Password");
                        editTextPassword.setText("");
                        passwordToConfirm = "";
                        password = "";
                        counterButton = 0;
                        startActivity(intent);
                    }
                    break;
            }
        }
    });
}

我猜想sharedPreference存在问题,但我不明白。

3 个答案:

答案 0 :(得分:0)

首先,您将获得像这样的共享首选项

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

稍后设置密码时,您将获得以下密码

SharedPreferences sharedPreferences = getSharedPreferences("PASSWORDS", 0);

它们是两个不同的共享首选项。使用第一个sharedpreferences。 有关更多信息,请参阅此页面https://developer.android.com/training/data-storage/shared-preferences

答案 1 :(得分:0)

轻松使用下面的库来管理数据

https://github.com/orhanobut/hawk

答案 2 :(得分:0)

首先,检查counterButton的值并确保执行情况2分支。 其次,用于设置的sharedPreferences和用于获取的sharedPreferences应该相同。您可以这样:

private final static String MyPREFERENCES = "MyPrefs"; //define two class fields
private SharedPreferences mSharedPreferences;

mSharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);//in activity Oncreate() methods. 


//and set or get in other place as you want.