zoukankan      html  css  js  c++  java
  • android中sharedPreferences的笔记

    haredPreferences的使用非常简单,能够轻松的存放数据和读取数据。SharedPreferences只能保存简单类型的数据,例如,String、int等。一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存。

    使用SharedPreferences保存key-value对的步骤如下:

    (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。

    (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。

    (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。

    (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。

    public static final String KEY_TOKEN = "token";
        public static final String APP_ID = "com.zhulang.secret";
        //读取
        public static String getCachedToken(Context context)
        {
            
            return context.getSharedPreferences(APP_ID,context.MODE_PRIVATE).getString(KEY_TOKEN, null);
            
        }
    //存储token.
        public static void CachedToken( Context context ,String token)
        {
            
            Editor et = context.getSharedPreferences(APP_ID,context.MODE_PRIVATE).edit();
            
            et.putString(KEY_TOKEN,token);
            et.commit();
            
        }
  • 相关阅读:
    wcf通道Channel
    固定位置右下角
    小闹钟(无样式)
    CSS小注意(初级)
    java少包汇总
    maven的pom.xml配置
    myeclipse 手动安装 lombok
    Could not synchronize database state with session
    (转)myeclipse插件—SVN分支与合并详解【图】
    Nginx的启动、停止与重启
  • 原文地址:https://www.cnblogs.com/ganwei/p/4544608.html
Copyright © 2011-2022 走看看