Android/iOS轻量级存储(账号密码等)

来源:互联网 发布:ubuntu 列出所有用户 编辑:程序博客网 时间:2024/06/05 16:15

开发中需要用到记住账号密码,在此总结一下。

Android多数会用SharePreferences来存储账号密码,在此记录一下自己简单做的简单数据存储:

/** * Created by ws on 16/7/28. */public class PreferenceUtils {    private static final  String SP_NAME ="BN_SP";    public  static SharedPreferences getSharePreferece(Context context){        return context.getApplicationContext().getSharedPreferences(SP_NAME,Context.MODE_PRIVATE);    }    public static void store(Context context,String key, Object value){        if (value instanceof Integer){            store(context,key,(Integer)value);        } else  if (value instanceof Long ){            store(context,key,(Long)value);        } else if (value instanceof Boolean){            store(context,key,(Boolean)value);        } else if (value instanceof String){            store(context,key,(String)value);        } else if (value instanceof Float){            store(context,key,(Float)value);        } else {            BmUtils.debug("store to sharedprefenrence error, value must be fundamental type! eg:(int,long,boolean,string,float)");            BmUtils.debug("key="+key+",value ="+value);        }    }    public  static void store(Context context,String key,String value){        SharedPreferences.Editor editor =getSharePreferece(context).edit();        editor.putString(key,value);        editor.commit();    }    public static void store(Context context,String key,Integer value){        SharedPreferences.Editor editor= getSharePreferece(context).edit();        editor.putInt(key,value);        editor.commit();    }    public static void store(Context context,String key,Long value){        SharedPreferences.Editor editor =getSharePreferece(context).edit();        editor.putLong(key,value);        editor.commit();    }    public static void store(Context context,String key,Boolean value){        SharedPreferences.Editor editor =getSharePreferece(context).edit();        editor.putBoolean(key,value);        editor.commit();    }    public static void store(Context context,String key,Float value){        SharedPreferences.Editor editor =getSharePreferece(context).edit();        editor.putFloat(key,value);        editor.commit();    }    public static String getString(Context context,String key){        return  getSharePreferece(context).getString(key,"");    }    public static int getInt(Context context,String key){        return  getSharePreferece(context).getInt(key,0);    }    public static long getLong(Context context,String key){        return  getSharePreferece(context).getLong(key,0l);    }    public static boolean getBoolean(Context context,String key){        return getSharePreferece(context).getBoolean(key,false);    }    public static float getFloat(Context context,String key){        return getSharePreferece(context).getFloat(key,0.0f);    }}
分别有String,Int,Long,Boolean,Float。使用中可以灵活调用,存取比较方便;

在iOS中使用的是NSUserDefaults来存储账号密码。下次记录下来

0 0
原创粉丝点击