我認爲這樣做最簡單,最直接的方法是保持截止日期:
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();
然後檢查
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();
}
但是,@CommonsWare是正確的,並保持信息只是你的過程更加正確。
謝謝,那就做了 –