您好,下面的活动将使您轻松登录!凭借这种积极性,我在下面的代码中遇到了两个问题
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();
}
}
}
答案 0 :(得分:1)
为什么您的值存储不正确的问题是:
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
}