Android凭据管理错误



嗨,下面的活动可以进行简单的登录!有了这个活动,我在下面的代码上有这两个问题

1(SharedPreferences:登录用户后,用户凭据应保持保存状态,以备下次启动时使用,但这仅适用于第一次,当用户第二次关闭应用程序时,应用程序不维护保存的凭据

2(保存的登录服务:我想禁用在各种服务中保存密码和用户名的请求,以保存登录数据!

我该如何解决这些问题?

Java代码:

package app;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import app.Controller.User;
import app.Service.Permissions;
import app.Service.Support;
import app.View.TabActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvUsername, tvPassword;
private EditText txtUsername, txtPassword;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
//Disabilita da StrictMode di android solo su sdk superiori a 9
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
/* Istanza dei campi */
tvUsername = (TextView) findViewById(R.id.usernametextview);
txtUsername = (EditText) findViewById(R.id.usernameeditext);
tvPassword = (TextView) findViewById(R.id.passwordtextview);
txtPassword = (EditText) findViewById(R.id.passwordtext);
final Button loginbutton = (Button) findViewById(R.id.buttonlogin);
//Configuro la funzione Listener sul login button
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
if (txtUsername.length() != 0 && txtPassword.length() != 0) {
if (Permissions.isOnline() == true) {
LoginFunction(txtUsername.getText().toString(), txtPassword.getText().toString(), true);
} else {
Support.Notification(MainActivity.this, "Errore", "Non stai navigando");
}
}
}
});

SharedPreferences sp1 = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
String userload = sp1.getString("Username", null);
String pwdload = sp1.getString("Password", null);
LoginFunction(userload, pwdload, false);
}
private void LoginFunction(String Username, String Password, Boolean ShowMessage) {
User u = new User();
Boolean ret = false;
try {
ret = u.Login(Username, Password);
if (ret == true) {
//Salvo le credenziali d'accesso in sharedPrefrences
SharedPreferences sp = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed = sp.edit();
Ed.putString("Username", txtUsername.getText().toString());
Ed.putString("Password", txtPassword.getText().toString());
Ed.commit();
//Creo un oggetto Intent da inviare ad una activity
Intent tabhome = new Intent(MainActivity.this, TabActivity.class);
tabhome.putExtra("User", u);
startActivity(tabhome);
} else {
if (ShowMessage == true) {
Support.Notification(MainActivity.this, "Errore", "Login non riuscito");
}
}
} catch (JSONException e) {
e.printStackTrace();
}

}

}

值未正确存储的问题是:

Ed.commit();

用代替

Ed.apply();

第二个2(有什么问题?

//编辑:

检查我的活动工作代码:

SharedPreferences prefs = getSharedPreferences(MyConstant.SHARED_PREFERENCES_NAME, MODE_PRIVATE);
if (prefs.getBoolean("firstTimeRun", true)) {
prefs.edit().putBoolean("firstTimeRun", false).apply();
// do something for the first time
}

最新更新