我已經創建了一個應用程序,其中一個用戶列表已放置在MySQL的數據庫中,分配給它們的用戶名,密碼和變量已經上傳。保持用戶在應用程序關閉時登錄 - Android Studio - MySQL
下面是登錄活動,它允許用戶登錄並進入他們自己的用戶區(還有一個loginrequest類也可以帶參數)。
我需要幫助的是讓應用程序保持登錄狀態,直到該用戶已經關閉應用程序爲止。
此外,如果可能,這將允許我發送用戶通知,當應用程序關閉個人用戶?
謝謝。
@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);
}
});
}
在'SharedPreferences'中保存一些值並在應用程序啓動時檢查它! –