我创建了一个应用程序,其中一个组的用户列表已经放在MySQL的数据库中,已经上传了分配给它们的用户名,密码和变量。
以下是允许用户登录并转到他们自己的用户区域的登录活动(还有一个loginrequest类也会使params结束)。
我需要帮助的是让应用程序保持登录状态,直到该用户注销,即使他们已关闭应用程序。
如果可能,这也允许我在个人用户关闭应用时发送用户通知吗?
谢谢你。@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView registerLink = (TextView) findViewById(R.id.tvRegisterHere);
//Register Here button
registerLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
//Login Button
bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Taking the username and password and converting it to a string
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//Taking the JSONObject from the 000webhost database
JSONObject jsonResponse = new JSONObject(response);
//If the response is successful ie is there a username and password that match
boolean success = jsonResponse.getBoolean("success");
if (success){
//Gather the information below
String name = jsonResponse.getString("name");
int var1 = jsonResponse.getInt("var1");
int var2 = jsonResponse.getInt("var2");
int var3 = jsonResponse.getInt("var3");
int var4 = jsonResponse.getInt("var4");
Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
intent.putExtra("name", name);
intent.putExtra("username", username);
intent.putExtra("var1", var1);
intent.putExtra("var2", var2);
intent.putExtra("var3", var3);
intent.putExtra("var4", var4);
intent.putExtra("password", password);
//Start the User Area Activity
LoginActivity.this.startActivity(intent);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
}
答案 0 :(得分:1)
您可以使用SharedPreferences来解决问题。
使用此选项验证用户是否已启用“会话”
private SharedPreferences sharedpreferences;
sharedpreferences=getApplicationContext().getSharedPreferences("Preferences", 0);
String login = sharedpreferences.getString("LOGIN", null);
if (login != null) {
//put your code if user is logged. For example, go to another activity
}else {
// to go login activity
}
单击登录按钮
时,使用此选项设置“会话” SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("LOGIN", newUser.getEmail_user());
editor.commit
在退出按钮上使用
Editor editor = sharedpreferences.edit();
editor.remove("LOGIN");
editor.commit();