Wpisz SharedPreferences do sporządzania roztworu,
EG.
public class Pref_Storage {
private static SharedPreferences sharedPreferences = null;
public static void openPref(Context context) {
sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.app_name),
Context.MODE_PRIVATE);
}
public static void deleteKey(Context context, String key) {
HashMap<String, String> result = new HashMap<String, String>();
Pref_Storage.openPref(context);
for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
.entrySet()) {
result.put(entry.getKey(), (String) entry.getValue());
}
boolean b = result.containsKey(key);
if (b) {
Pref_Storage.openPref(context);
Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
prefsPrivateEditor.remove(key);
prefsPrivateEditor.commit();
prefsPrivateEditor = null;
Pref_Storage.sharedPreferences = null;
}
}
public static void setDetail(Context context, String key, String value) {
Pref_Storage.openPref(context);
Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
prefsPrivateEditor.putString(key, value);
prefsPrivateEditor.commit();
prefsPrivateEditor = null;
Pref_Storage.sharedPreferences = null;
}
public static Boolean checkDetail(Context context, String key) {
HashMap<String, String> result = new HashMap<String, String>();
Pref_Storage.openPref(context);
for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
.entrySet()) {
result.put(entry.getKey(), (String) entry.getValue());
}
boolean b = result.containsKey(key);
return b;
}
public static String getDetail(Context context, String key) {
HashMap<String, String> result = new HashMap<String, String>();
Pref_Storage.openPref(context);
for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
.entrySet()) {
result.put(entry.getKey(), (String) entry.getValue());
}
String b = result.get(key);
return b;
}
}
Zastosowanie:
przed check logowanie login_flag:
if (Pref_Storage.checkDetail(getApplicationContext(), "login_flag"))
{
// Home Screen
}
else
{
//Display Login Screen
}
Po zalogowaniu ustawić login_flag:
Pref_Storage.setDetail(getApplicationContext(), "login_flag", "0");
automatyczne wylogowanie po 5 minut? Wyraźnie wygasaj token logowania co 5 minut, chyba że użytkownik wykona jakąś akcję na telefonie. W takim przypadku odnów token na następne 5 minut. – Rohit5k2
Najlepsze praktyki zostały uwzględnione w sytuacjach, w których telefon użytkownika jest zagubiony i nadal jest zalogowany. Odmawianie logowania to oczywiście utrata potencjału w tym przypadku! Zacznę od wymieniania wszystkich możliwych sytuacji i ważenia zalet i wad każdej powierzchni! –
tak Narasimhan. te scenariusze są tym, czego szukam. użytkownik jest zalogowany i jego telefon jest zgubiony/formatuje/odinstaluj aplikację. jak sobie poradzę z takimi sytuacjami. jednym z nich jest to, że jeśli użytkownik loguje się z drugiego urządzenia, to jest on automatycznie wylogowywany z pierwszego urządzenia. Drugie rozwiązanie polega na tym, że jeśli jest zalogowany z 1 miejsca i próbuje później zalogować się z innego miejsca, należy o tym poinformować pocztą elektroniczną. ale muszę przechowywać identyfikator urządzenia w db również – iGod