对密码使用SharedReferences-android
先生,如何在其他类中访问我的SharedReferences文件?我希望应用程序有一个默认密码1234,然后如果用户想要更改它。我第一次使用了这段代码,但当我重新启动应用程序时,它将恢复为旧密码。提前谢谢你的帮助对密码使用SharedReferences-android,android,sharedpreferences,Android,Sharedpreferences,先生,如何在其他类中访问我的SharedReferences文件?我希望应用程序有一个默认密码1234,然后如果用户想要更改它。我第一次使用了这段代码,但当我重新启动应用程序时,它将恢复为旧密码。提前谢谢你的帮助 public class ChangePassword extends Activity { /** Called when the activity is first created. */ EditText enterPassword, newPassword; public fi
public class ChangePassword extends Activity {
/** Called when the activity is first created. */
EditText enterPassword, newPassword;
public final String filename = "PasswordFile";
String myPassword;
SharedPreferences myFolder;
public static String dataReturned = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.changepassword);
Button save = (Button)findViewById(R.id.btnSavePassword);
enterPassword = (EditText)findViewById(R.id.etCurrentPassword);
newPassword = (EditText)findViewById(R.id.etNewPassword);
myFolder = getSharedPreferences(filename, 0);//a folder
dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(""))
{
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", "1234"); //newData is new pass, passwordKey is key
editor.commit();
}
save.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
// TODO Auto-generated method stub
String stringData = enterPassword.getText().toString().trim();
dataReturned = myFolder.getString("passwordKey", ""); //key/def message
//if stored password is equal to entered password
if(dataReturned.equals(stringData))
{
String newData = newPassword.getText().toString().trim();
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", newData); //newData is new pass, passwordKey is key
editor.commit();
dataReturned = myFolder.getString("passwordKey", "couldn't load data"); //get file from sharedpref
Toast.makeText(getApplicationContext(),
"Password successfully changed",
Toast.LENGTH_LONG).show();
enterPassword.setText("");
newPassword.setText("");
}
else
{
Toast.makeText(getApplicationContext(),
"Wrong password!",
Toast.LENGTH_LONG).show();
enterPassword.setText("");
newPassword.setText("");
}
}
});
}//oncreate()
}
这里是我想在我的另一个类中使用SharedReferences中保存的数据的地方
public void dispatch_button_action(View view){
final EditText password_input = new EditText(this); // create an text input field
password_input.setHint("Enter Password"); // put a hint in it
password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type
AlertDialog.Builder alertDialog = new Builder(this); // create an alert box
alertDialog.setTitle("Enter Password"); // set the title
alertDialog.setView(password_input); // insert the password text field in the alert box
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button
public void onClick(DialogInterface dialog, int which) {
String entered_password = password_input.getText().toString();
String password = ChangePassword.myFolder.getString("passwordKey", "");
if(!password.trim().equals(""))
{
testPassAndSendSms(password.trim(), entered_password);
}
else
{
testPassAndSendSms(my_password, entered_password);
}
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show(); // show the alert box
}//end dispatch_button
当前,您没有保存用户在Alertbox中输入的最新密码,因此在另一个活动中,将最新密码保存在SharedReferences中,如下所示:
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String entered_password = password_input.getText().toString();
SharedPreferences myFolder = You_Current_Activitu.this.getSharedPreferences(filename, 0);
String dataReturned = myFolder.getString("passwordKey", "");
if(dataReturned.equals(""))
{
SharedPreferences.Editor editor = myFolder.edit();
editor.putString("passwordKey", entered_password);
editor.commit();
}
与您的根本问题无关:为什么在活动启动期间不保留共享首选项值,但为什么在没有任何保护的情况下将密码存储在共享pref中?有关如何在AndroidHere()上存储用户密码的提示,请参阅。此处()详细讨论了如何安全地存储凭据。因此,在我输入密码时,它将用作我的默认密码?我实际上希望默认密码为1234,那么用户可以选择是否更改它。@UsuiTakumi:然后只需更改
editor.putString(“passwordKey”,输入密码)
到编辑器.putString(“passwordKey”,“1234”)代码>