MySpHelper.java 4.01 KB
package com.example.modulepedometer.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * sharedPreferencesHelper 可以指定写入模式
 * //todo 其他人写的无法指定写入模式,这就造成,同一个APP的不同进程,访问的sp中的值不一致,造成保活服务没有可判断的标志位
 *
 * @author ShinnyYang
 */
public class MySpHelper {
    private static MySpHelper sharedPreferencesHelper = null;
    private static int SP_MODE;//指定模式

    //单例模式,把Context传进去
    public static MySpHelper getInstance(Context context, int MODE) {
        SP_MODE = MODE;
        if (sharedPreferencesHelper == null) {
            synchronized (MySpHelper.class) {
                if (sharedPreferencesHelper == null) {
                    sharedPreferencesHelper = new MySpHelper();
                    sharedPreferencesHelper.setContext(context);
                    return sharedPreferencesHelper;
                }
            }
        }

        return sharedPreferencesHelper;
    }

    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    //取Boolean 型数据
    public boolean getBoolean(String key, boolean defValue) {
        try {
            return getSP().getBoolean(key, defValue);
        } catch (NullPointerException exception) {
            Log.d("hcj", "" + exception);
            return defValue;
        }
    }

    //存Boolean 型数据
    public void putBoolean(String key, boolean value) {
        try {
            SharedPreferences.Editor editor = getSP().edit();
            editor.putBoolean(key, value);
            editor.commit();
        } catch (NullPointerException exception) {
            Log.d("hcj", "" + exception);
        }
    }

    //取Long 型数据
    public long getLong(String key, long defValue) {
        try {
            return getSP().getLong(key, defValue);
        } catch (NullPointerException exception) {
            Log.d("hcj", "" + exception);
            return defValue;
        }
    }

    //存Long 型数据
    public void putLong(String key, long value) {
        try {
            SharedPreferences.Editor editor = getSP().edit();
            editor.putLong(key, value);
            editor.commit();
        } catch (NullPointerException exception) {
            Log.d("hcj", "" + exception);
        }
    }

    //取整型
    public int getInt(String key, int defaultValue) {
        try {
            return getSP().getInt(key, defaultValue);
        } catch (Exception e) {
            Log.d("hcj", "" + e);
            return defaultValue;

        }
    }

    //存整型
    public void putInt(String key, int value) {
        try {
            SharedPreferences.Editor editor = getSP().edit();
            editor.putInt(key, value);
            editor.commit();
        } catch (Exception e) {
            Log.d("hcj", "" + e);
        }
    }

    //取String
    public String getString(String key, String defValue) {
        try {
            return getSP().getString(key, defValue);
        } catch (NullPointerException e) {
            Log.d("hcj", "" + e);
            return defValue;
        }
    }

    //存String
    public void putString(String key, String value) {
        try {
            SharedPreferences.Editor editor = getSP().edit();
            editor.putString(key, value);
            editor.commit();
        } catch (NullPointerException e) {
            Log.d("hcj", "" + e);
        }
    }

    //清除数据
    public void clear() {
        try {
            SharedPreferences.Editor editor = getSP().edit();
            editor.clear();
            editor.commit();
        } catch (NullPointerException e) {
            Log.d("hcj", "" + e);
        }
    }

    //获得SharedPreferences对象
    private SharedPreferences getSP() {
        return context.getSharedPreferences("sp", SP_MODE);
    }

    private SharedPreferences getSP(String name) {
        return context.getSharedPreferences(name, SP_MODE);
    }
}