gpt4 book ai didi

安卓。 App关闭后如何保存用户名和密码?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:39 24 4
gpt4 key购买 nike

我正在编写一个包含登录详细信息(用户名和密码)的应用程序。
我想这样做,以便当用户关闭应用程序并稍后再次启动时,他/她不必再次输入他/她的用户名和密码。

到目前为止,这是我的代码。不幸的是,它似乎不记得密码和用户名:

protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mPrefs = getSharedPreferences(PREFS, 0);
final CheckBox rememberMeCbx = (CheckBox)findViewById(R.id.saveLoginCheckBox);

boolean rememberMe = mPrefs.getBoolean("rememberMe", false);



if (android.os.Build.VERSION.SDK_INT > 9)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
mXmlRpcClient = new XMLRPCClient(mXmlRpcUri);

// Set up the login form.
//mUsername = getIntent().getStringExtra(EXTRA_EMAIL);
mUsernameView = (EditText) findViewById(R.id.username);
mUsernameView.setText(mUsername);

mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id,
KeyEvent keyEvent) {
if (id == R.id.login || id == EditorInfo.IME_NULL) {
attemptLogin();
return true;
}
return false;
}
});

mLoginFormView = findViewById(R.id.login_form);
mLoginStatusView = findViewById(R.id.login_status);
mLoginStatusMessageView = (TextView) findViewById(R.id.login_status_message);

findViewById(R.id.sign_in_button).setOnClickListener
(
new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if(rememberMeCbx.isChecked())
{
attemptLogin(); //function to check if fields are filled correctly
saveLoginDetails();
}
else
{
attemptLogin();
removeLoginDetails();
}

}
});

if(rememberMe == true)
{
//get previously stored login details
String login = mPrefs.getString("mUsername", null);
String upass = mPrefs.getString("mPassword", null);

if(login != null && upass != null)
{
//fill input boxes with stored login and pass
EditText loginEbx = (EditText)findViewById(R.id.username);
EditText passEbx = (EditText)findViewById(R.id.password);
loginEbx.setText(login);
passEbx.setText(upass);

//set the check box to 'checked'
rememberMeCbx.setChecked(true);
}
}
}



/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
mSessionID = (String)mXmlRpcClient.call(mLoginFuncName, mUsername, mPassword);
return true;
} catch (Exception e) {
return false;
}
}

@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
showProgress(false);

if (success) {
finish();
Intent intent = new Intent(LoginActivity.this, MainWindow.class);
intent.putExtra("SessionID", mSessionID);
intent.putExtra("XmlRpcUrl", mXmlRpcUrl);
intent.putExtra("LoginFuncName", mLoginFuncName);
intent.putExtra("LogoutFuncName", mLogoutFuncName);
intent.putExtra("GetDevicesFuncName", mGetDevicesFuncName);
intent.putExtra("SendPositionFuncName", mSendPositionFuncName);
intent.putExtra("GetSavedTripFunc", mGetSavedTripFunc);
startActivity(intent);
} else {
mPasswordView
.setError(getString(R.string.error_incorrect_password));
mPasswordView.requestFocus();
}
}


}

private void saveLoginDetails()
{
//fill input boxes with stored login and pass
EditText loginEbx = (EditText)findViewById(R.id.username);
EditText passEbx = (EditText)findViewById(R.id.password);
String login = loginEbx.getText().toString();
String upass = passEbx.getText().toString();

Editor e = mPrefs.edit();
e.putBoolean("rememberMe", true);
e.putString("login", login);
e.putString("password", upass);
e.commit();
}

private void removeLoginDetails()
{
Editor e = mPrefs.edit();
e.putBoolean("rememberMe", false);
e.remove("login");
e.remove("password");
e.commit();
}

你能告诉我我的代码有什么问题吗?我该如何改进它,以便在应用程序关闭并再次打开后保存和检索用户名和密码?

最佳答案

尝试这种方式:首先定义Preferences

private static final String PREFS_NAME = "preferences";
private static final String PREF_UNAME = "Username";
private static final String PREF_PASSWORD = "Password";

private final String DefaultUnameValue = "";
private String UnameValue;

private final String DefaultPasswordValue = "";
private String PasswordValue;

onPause()

@Override
public void onPause() {
super.onPause();
savePreferences();

}

onResume()

@Override
public void onResume() {
super.onResume();
loadPreferences();
}

这里是savePreferences()

private void savePreferences() {
SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();

// Edit and commit
UnameValue = edt_username.getText();
PasswordValue = edt_password.getText();
System.out.println("onPause save name: " + UnameValue);
System.out.println("onPause save password: " + PasswordValue);
editor.putString(PREF_UNAME, UnameValue);
editor.putString(PREF_PASSWORD, PasswordValue);
editor.commit();
}

这里是 loadPreferences()

private void loadPreferences() {

SharedPreferences settings = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);

// Get value
UnameValue = settings.getString(PREF_UNAME, DefaultUnameValue);
PasswordValue = settings.getString(PREF_PASSWORD, DefaultPasswordValue);
edt_username.setText(UnameValue);
edt_password.setText(PasswordValue);
System.out.println("onResume load name: " + UnameValue);
System.out.println("onResume load password: " + PasswordValue);
}

关于安卓。 App关闭后如何保存用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21697172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com