zoukankan      html  css  js  c++  java
  • 4.25

    SharedPreference工具类:

    SPUtils.java

    package com.jay.sharedpreferencedemo3;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import java.util.Map;
    
    /**
     * Created by Jay on 2015/9/2 0002.
     */
    public class SPUtils {
        /**
         * 保存在手机里的SP文件名
         */
        public static final String FILE_NAME = "my_sp";
    
        /**
         * 保存数据
         */
        public static void put(Context context, String key, Object obj) {
            SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            if (obj instanceof Boolean) {
                editor.putBoolean(key, (Boolean) obj);
            } else if (obj instanceof Float) {
                editor.putFloat(key, (Float) obj);
            } else if (obj instanceof Integer) {
                editor.putInt(key, (Integer) obj);
            } else if (obj instanceof Long) {
                editor.putLong(key, (Long) obj);
            } else {
                editor.putString(key, (String) obj);
            }
            editor.commit();
        }
    
    
        /**
         * 获取指定数据
         */
        public static Object get(Context context, String key, Object defaultObj) {
            SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
            if (defaultObj instanceof Boolean) {
                return sp.getBoolean(key, (Boolean) defaultObj);
            } else if (defaultObj instanceof Float) {
                return sp.getFloat(key, (Float) defaultObj);
            } else if (defaultObj instanceof Integer) {
                return sp.getInt(key, (Integer) defaultObj);
            } else if (defaultObj instanceof Long) {
                return sp.getLong(key, (Long) defaultObj);
            } else if (defaultObj instanceof String) {
                return sp.getString(key, (String) defaultObj);
            }
            return null;
        }
    
        /**
         * 删除指定数据
         */
        public static void remove(Context context, String key) {
            SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.remove(key);
            editor.commit();
        }
    
    
        /**
         * 返回所有键值对
         */
        public static Map<String, ?> getAll(Context context) {
            SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);Map<String,?> map = sp.getAll();return map;}/**
         * 删除所有数据
         */publicstaticvoid clear(Context context){SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();
            editor.clear();
            editor.commit();}/**
         * 检查key对应的数据是否存在
         */publicstaticboolean contains(Context context,String key){SharedPreferences sp = context.getSharedPreferences(FILE_NAME, context.MODE_PRIVATE);return sp.contains(key);}}
  • 相关阅读:
    Python安装及编辑器UliPad安装
    安装配置Django开发环境(Eclipse + Pydev)
    VsSDK_sfx.exe 安装出错
    [转]代码分析工具FxCop1.36之一:介绍与使用
    [转]IBM Rational系列产品介绍
    [转]C#控件——DataGridView单元格文本自动换行
    [转]FrameWork4.0的兼容问题 .
    【整理】C# ToString格式字符串整理(Format)(数字、日期和枚举的标准格式设置说明符)(SamWang)
    [转]史上最好的20本敏捷开发书籍
    [转]C#数字千分位问题
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14909793.html
Copyright © 2011-2022 走看看