gpt4 book ai didi

android - 移动应用程序共享首选项保存和调用用户登录

转载 作者:行者123 更新时间:2023-11-29 19:46:00 27 4
gpt4 key购买 nike

目前我的应用程序正在“设备”表中搜索现有设备以启用登录访问。但是,我想在他们每次需要登录时注册一个新设备,并让他们在每次应用程序运行时绕过登录页面。目前,该应用程序在打开时表示凭据无效,因为用户名和密码未正确保存。我该如何解决这个问题?

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

// declaring variables
etUsername = (EditText)findViewById(R.id.etUsername);
etPassword= (EditText)findViewById(R.id.etPassword);
btnLogin = (Button)findViewById(R.id.btnLogin);
etIpAddress = (EditText) findViewById(R.id.etIpAddress);
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String ipAddress = etIpAddress.getText().toString();

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
if (sharedPreferences.contains("ip")) {
performLogin(username, password, sharedPreferences.getString("ip", ipAddress));
}

// setting up things for login button
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String ipAddress = etIpAddress.getText().toString();

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);

sharedPreferences.edit()
.putString("ip", ipAddress)
.apply();

String username = etUsername.getText().toString().trim();
String password = etPassword.getText().toString().trim();

performLogin(username, password, ipAddress);
}
});
}

private void performLogin(String username, String password, String ipAddress) {
try {
Device.login(username, password, ipAddress, this);
} catch (JSONException e) {
onLoginFailure(e);
}
}

最佳答案

为了保存登录凭据,我们始终遵循以下代码:-public static final String MyPREFERENCES = "MyPrefs";

   public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String Email = "ip";
SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString(Name, n);
editor.putString(Phone, ph);
editor.putString(ip, e);
editor.commit();

我认为你必须在你的代码中使用那个东西而不是 apply() 方法

关于android - 移动应用程序共享首选项保存和调用用户登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37696925/

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