android sharedpreferences 工具类,SharedPreferences工具类

[android]代码库package com.mosjoy.ad.utils;

import java.util.Map;

import java.util.Set;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

/**

* SharedPreferences工具类

* @author zouyb

*

*/

public class SharedPreferencesUtil {

private SharedPreferences sp;

private Editor editor;

private String name = "mosjoy_chat";

private int mode = Context.MODE_PRIVATE;

public SharedPreferencesUtil(Context context) {

this.sp = context.getSharedPreferences(name, mode);

this.editor = sp.edit();

}

/**

* 创建一个工具类,默认打开名字为name的SharedPreferences实例

* @param context

* @param name 唯一标识

* @param mode 权限标识

*/

public SharedPreferencesUtil(Context context, String name, int mode) {

this.sp = context.getSharedPreferences(name, mode);

this.editor = sp.edit();

}

/**

* 添加信息到SharedPreferences

*

* @param name

* @param map

* @throws Exception

*/

public void add(Map map) {

Set set = map.keySet();

for (String key : set) {

editor.putString(key, map.get(key));

}

editor.commit();

}

/**

* 删除信息

*

* @throws Exception

*/

public void deleteAll() throws Exception {

editor.clear();

editor.commit();

}

/**

* 删除一条信息

*/

public void delete(String key){

editor.remove(key);

editor.commit();

}

/**

* 获取信息

*

* @param key

* @return

* @throws Exception

*/

public String get(String key){

if (sp != null) {

return sp.getString(key, "");

}

return "";

}

/**

* 获取此SharedPreferences的Editor实例

* @return

*/

public Editor getEditor() {

return editor;

}

}

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值