Android中使用SharedPreferences封装的本地读取类

开发中遇到的 基于sharedpreference对本地信息的处理


代码部分:

package pro_util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by 小林哥 on 2016/10/14.
 * 用于将一些信息保存在本地的xml文件中
 */
public class ShareUtil {
    public static SharedPreferences getSharedPreferences(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp;
    }

    public static void save(String key, Object value, Context context) {
        SharedPreferences.Editor e = getSharedPreferences(context).edit();
        if (value instanceof String) {
            e.putString(key, (String) value);
        } else if (value instanceof Integer) {
            e.putInt(key, (Integer) value);
        } else if (value instanceof Boolean) {
            e.putBoolean(key, (Boolean) value);
        } else if (value instanceof Long) {
            e.putLong(key, (Long) value);
        }
        e.commit();
    }
    public static String getName(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString("name", "username");
    }

    /**
     * 获取是否第一次登录
     *
     * @param context
     * @return
     */
    public static Boolean getIsFrist(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean("isFirst",true);
    }
    //获取用户名
    public static String getAccount(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString("Account",null);
    }
    //获取密码
    public static String getPassword(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString("passWord",null);
    }
    //移除一些设定的属性
    public static void remove(String key,Context context){
        SharedPreferences sp = getSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }

    public static boolean isAaronLiInfo(Context context){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean("isAaronLiInfo",false);
    }

    /**
     * 勿扰模式 获得开始小时
     *
     * @param context
     * @return
     */
    public static int getBeginHour(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt("beginHour",00);
    }

    /**
     * 勿扰模式 获得开始分钟
     *
     * @param context
     * @return
     */
    public static int getBeginMin(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt("beginMin",00);
    }

    /**
     * 勿扰模式 获得结束小时
     *
     * @param context
     * @return
     */
    public static int getEndHour(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt("endHour",00);
    }

    /**
     * 勿扰模式 获得结束分钟
     *
     * @param context
     * @return
     */
    public static int getEndMin(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt("endMin",00);
    }

    /**
     * 是否开启声音
     * @param context
     * @return
     */
    public static boolean openVoice(Context context){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean("voice",false);
    }

    /**
     *是否开启震动
     * @param context
     * @return
     */
    public static boolean openVibrate(Context context){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean("vibration",false);
    }

    /**
     *是否开启通知详情
     * @param context
     * @return
     */
    public static boolean openNoticeInfo(Context context){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean("flags",false);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值