安卓SP(SharedPreferences)


```java
package com.allynav.systemservice.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * app私有目录下保存SharedPreferences文件
 */
public class SharedUtil {

	/**
	 * 将map集合中的所有键值对都保存,map的key对应保存的key value对应保存的value
	 *
	 * @param context
	 * @param fileName
	 * @param map
	 */
	public static void putMap(Context context, String fileName, Map<String, String> map) {
		if (map != null && map.size() > 0) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			Editor editor = preferences.edit();
			Set<String> keySet = map.keySet();
			Iterator<String> iterator = keySet.iterator();
			while (iterator.hasNext()) {
				String key = iterator.next();
				String value = map.get(key);
				editor.putString(key, value);
			}
			editor.commit();
		}
	}

	/**
	 * 保存一个String
	 *
	 * @param context
	 * @param fileName
	 *            保存的文件名
	 * @param key
	 * @param value
	 */
	public static void putString(Context context, String fileName, String key, String value) {
		SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 获取一个String
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static String getString(Context context, String fileName, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			return preferences.getString(key, "");
		}
		return "";
	}

	/**
	 * 保存boolean
	 *
	 * @param context
	 * @param key
	 * @param value
	 */
	@SuppressLint("NewApi")
	public static void putBoolean(Context context, String fileName, String key, boolean value) {
		WeakReference<SharedPreferences> weakpreferences = new WeakReference<SharedPreferences>(
				context.getSharedPreferences(fileName, Context.MODE_PRIVATE));
		SharedPreferences preferences = weakpreferences.get();
		Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.apply();
	}

	/**
	 * 获取boolean值<br>
	 * <p>
	 * 若找不到此值默认返回 false
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(Context context, String fileName, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			return preferences.getBoolean(key, false);
		}
		return false;
	}

	// /
	// // 保存在默人文件中的Api /
	// 

	/**
	 * 将map集合中的所有键值对都保存在默认的文件中,map的key对应保存的key value对应保存的value
	 *
	 * @param context
	 * @param map
	 */
	public static void putMap(Context context, Map<String, String> map) {
		if (map != null && map.size() > 0) {
			SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
					Context.MODE_PRIVATE);
			Editor editor = preferences.edit();
			Set<String> keySet = map.keySet();
			Iterator<String> iterator = keySet.iterator();
			while (iterator.hasNext()) {
				String key = iterator.next();
				String value = map.get(key);
				editor.putString(key, value);
			}
			editor.commit();
		}
	}

	/**
	 * 采用默认的文件名保存数据,默认以PackageName为文件名
	 *
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putString(Context context, String key, String value) {
		SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 获取默认保存文件中的数据,默认以PackageName为文件名
	 *
	 * @param context
	 * @param key
	 * @return
	 */
	public static String getString(Context context, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
					Context.MODE_PRIVATE);
			return preferences.getString(key, "");
		}
		return "";
	}

	/**
	 * 采用默认的文件名保存数据,默认以PackageName为文件名
	 *
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putBoolean(Context context, String key, boolean value) {
		SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 获取默认保存文件中的数据,默认以PackageName为文件名<br>
	 * <p>
	 * 若找不到此值默认返回 false
	 *
	 * @param context
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(Context context, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(context.getPackageName(),
					Context.MODE_PRIVATE);
			return preferences.getBoolean(key, false);
		}
		return false;
	}

	/**
	 * @param context
	 * @param fileName
	 * @param key
	 * @param value
	 */
	public static void putLong(Context context, String fileName, String key, long value) {
		SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/**
	 * 获取long
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static long getLong(Context context, String fileName, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			return preferences.getLong(key, -1);
		}
		return -1;
	}

	/**
	 * 保存int
	 *
	 * @param context
	 * @param key
	 * @param value
	 */
	@SuppressLint("NewApi")
	public static void putInt(Context context, String fileName, String key, int value) {
		WeakReference<SharedPreferences> weakpreferences = new WeakReference<SharedPreferences>(
				context.getSharedPreferences(fileName, Context.MODE_PRIVATE));
		SharedPreferences preferences = weakpreferences.get();
		Editor editor = preferences.edit();
		editor.putInt(key, value);
		editor.apply();
	}

	/**
	 * 获取Int
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static int getInt(Context context, String fileName, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			return preferences.getInt(key, 0);
		}
		return 0;
	}

	/**
	 * 保存float
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @param value
	 */
	public static void putFloat(Context context, String fileName, String key, float value) {
		SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		Editor editor = preferences.edit();
		editor.putFloat(key, value);
		editor.commit();
	}

	/**
	 * 获取float
	 *
	 * @param context
	 * @param fileName
	 * @param key
	 * @return
	 */
	public static float getFloat(Context context, String fileName, String key) {
		if (context != null) {
			SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
			return preferences.getFloat(key, 0);
		}
		return 0;
	}

	public static boolean clearAllValue(Context context, String fileName) {
		SharedPreferences preferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return preferences.edit().clear().commit();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迪霸LZTXDY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值