当前位置:   article > 正文

android本地存储工具类封装_android 存储路径工具类

android 存储路径工具类
package com.coral3.common_module.utils;

import android.app.Application;
import android.content.SharedPreferences;

/**
 * @author 蓝之静云
 * @description 本地存储工具类
 * @date 2021-12-*
 */
public class SharedPrefUtils {

    private static SharedPrefUtils instance;
    private SharedPreferences sp;

    private SharedPrefUtils(Application application) {
        sp = application.getSharedPreferences(
                application.getPackageName(), 0);
    }

    public static SharedPrefUtils getInstance() {
        if (instance == null) {
            synchronized (SharedPrefUtils.class) {
                if (instance == null) {
                    instance = new SharedPrefUtils((Application) InitUtil.getContext().getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void put(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    public String get(String key, String defValue) {
        return sp.getString(key, defValue);
    }

    public void put(String key, Integer value) {
        sp.edit().putInt(key, value).commit();
    }

    public Integer get(String key, Integer defValue) {
        return sp.getInt(key, defValue);
    }

    public void put(String key, Float value) {
        sp.edit().putFloat(key, value).commit();
    }

    public Float get(String key, Float defValue) {
        return sp.getFloat(key, defValue);
    }

    public void put(String key, Long value) {
        sp.edit().putLong(key, value).commit();
    }

    public Long get(String key, Long defValue) {
        return sp.getLong(key, defValue);
    }

    public void put(String key, boolean value) {
        sp.edit().putBoolean(key, value).commit();
    }

    public Boolean get(String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public void remove(String key) {
        try {
            sp.edit().remove(key).commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/319211
推荐阅读
相关标签
  

闽ICP备14008679号