Android本地数据存储(CacheDataUtil )

该博客介绍了如何在Android中进行本地数据存储,特别是利用CacheDataUtil和CacheUtil工具类进行缓存操作。首先检查SD卡是否存在,然后演示了如何保存和获取数据到本地,以实现数据的持久化存储。

判断sd卡是否存在:

if(!CacheService.isSdCardExist()){
Toast.makeText(UserLoginActivity.this, "未找到SD卡",
Toast.LENGTH_SHORT).show();
}

存数据:


CacheDataUtil.saveCacheData(dataList,Constants.DATA_CUSTOMER_LIST);


取数据:

dataList=CacheDataUtil.getCacheData(this, Constants.DATA_CUSTOMER_LIST);


使用的两个工具类分别是:CacheDataUtil,CacheUtil


public class CacheUtil {

private static final long INITIALCRC = 0xFFFFFFFFFFFFFFFFL; private static long[] CRCTable = new long[256]; private static boolean init = false; private static final long POLY64REV = 0x95AC9329AC4BC9B5L; /** * A function thats returns a 64-bit crc for string * @param in *            : input string * @return 64-bit crc value */ public static final long Crc64Long(String in) { if (in == null || in.length() == 0) { return 0; } // http://bioinf.cs.ucl.ac.uk/downloads/crc64/crc64.c long crc = INITIALCRC, part; if (!init) { for (int i = 0; i < 256; i++) { part = i; for (int j = 0; j < 8; j++) { int value = ((int) part & 1); if (value != 0) part = (part >> 1) ^ POLY64REV; else part >>= 1; } CRCTable[i] = part; }
			init = true;
		}
		int length = in.length();
		for (int k = 0; k < length; ++k) {
			char c = in.charAt(k);
			crc = CRCTable[(((int) crc) ^ c) & 0xff] ^ (crc >> 8);
		}
		return crc;
	}

}

CacheDataUtil 工具类

/**
 * 本地缓存处理方法
 * @author Administrator
 *
 */

public class CacheDataUtil {

/** * 保存对象数组到本地 */ public static boolean saveCacheData(ArrayList<Parcelable> dataList, String Key){ Long cacheKey = CacheUtil.Crc64Long(Key); CacheService.sDataCache.delete(cacheKey); if (dataList.size() > 0 && cacheKey != 0 && CacheService.sDataCache != null) { CacheService.sDataCache.flush(); Parcel parcel = Parcel.obtain(); parcel.writeList(dataList); byte[] bytes = parcel.marshall(); if (bytes != null && bytes.length > 0) { CacheService.sDataCache.put(cacheKey,bytes, 0); CacheService.sDataCache.flush(); } parcel.recycle(); } return true; } /** * 获取本地所有的对象 */ @SuppressWarnings("unchecked") public static ArrayList<Parcelable> getCacheData(Context context,String Key){ ArrayList<Parcelable> list = null; // 首先读取用户相关门店缓存应用信息 Long cacheKey = CacheUtil.Crc64Long(Key); byte[] datas = CacheService.sDataCache != null ? CacheService.sDataCache .get(cacheKey,0) : null; if (datas != null && datas.length > 0) { Parcel parcel = Parcel.obtain(); parcel.unmarshall(datas, 0, datas.length); parcel.setDataPosition(0); list = parcel.readArrayList(context.getClass().getClassLoader()); parcel.recycle(); } if (list == null) { list = new ArrayList<Parcelable>(); }else{ Iterator<Parcelable> iterator = list.iterator(); while(iterator.hasNext()){ if(null==iterator.next()){ iterator.remove(); } } } return list; } /** * 清除 */ public static void clearAllCacheData() { CacheService.sDataCache.deleteAll();; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值