How to maintain session in android?

后端 未结 10 2003
轻奢々
轻奢々 2020-11-29 19:35

Can anybody tell me how to maintain session for a user login. For example when the user sign- in to an application they have to be signed in unless the user logouts or unin

10条回答
  •  清歌不尽
    2020-11-29 20:15

    Using this class will help you to store all types of sessions

    public class Session {
    
        private SharedPreferences prefs;
    
        public Session(Context cntx) {
            // TODO Auto-generated constructor stub
            prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
        }
    
        public void set(String key,String value) {
            prefs.edit().putString(key, value).commit();
        }
    
        public String get(String key) {
            String value = prefs.getString(key,"");
            return value;
        }
    }
    

提交回复
热议问题