I want to store login data but I want that data to be deleted after 60 minutes. What is the proper way to do this?
The app can be closed, stopped, opened in these 60 minutes. I don't want to use an internal database.
Here is my code for accessing SharedPreferences
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Username, usernameTxt);
editor.putString(Password, passwordTxt);
I think the easiest and most direct way to do this is keeping expired date:
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("Username", usernameTxt);
editor.putString("Password", passwordTxt);
editor.putLong("ExpiredDate", System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(60));
editor.apply();
And then check it
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.getLong("ExpiredDate", -1) > System.currentTimeMillis()) {
// read email and password
} else {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.apply();
}
But, @CommonsWare is right and keeping the information just in your process is more correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With