How to make user login from only one device at a time

后端 未结 7 2879
予麋鹿
予麋鹿 2021-02-04 16:47

I have a RestAPI which when hit authenticate the user. This api is exposed to android and ios developers they hit this api to allow user to login to our app

My requireme

7条回答
  •  花落未央
    2021-02-04 17:28

    Use SharedPreferences for solution,

    For 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 result = new HashMap();
    
            Pref_Storage.openPref(context);
            for (Entry 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 result = new HashMap();
    
            Pref_Storage.openPref(context);
            for (Entry 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 result = new HashMap();
    
            Pref_Storage.openPref(context);
            for (Entry entry : Pref_Storage.sharedPreferences.getAll()
                    .entrySet()) {
                result.put(entry.getKey(), (String) entry.getValue());
            }
    
            String b = result.get(key);
            return b;
    
        }
    }
    

    Use:

    Before login check login_flag:

    if (Pref_Storage.checkDetail(getApplicationContext(), "login_flag"))
    {
        // Home Screen
    }
    else
    {
        //Display Login Screen
    }
    

    After Login set login_flag:

    Pref_Storage.setDetail(getApplicationContext(), "login_flag", "0");
    

提交回复
热议问题