編輯:關於Android編程
而不去請求網絡,實現更好的用戶體驗,但是這些數據只能保存一定時間,而且當APP關閉時,這些數據就得銷毀,
查了半天API貌似沒看到定時緩存(不確定到底有沒有),這裡就自己模擬了一個,注釋已經很詳細了。。。
看代碼:
package com.memorycache;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import com.bxg.news.utils.DateUtils;
/**
* 把一些數據緩存到內存,然後設置定時清理功能
*
* @author JiangYueSong
*
*/
public class CacheContainer {
/**
* 緩存的map
*/
private static Map cacheFile = new LinkedHashMap();
/**
* 緩存只能占用的最大堆內存
*/
private static long limit = 1000000;
/**
* 緩存已經使用的大小
*/
private static long usedCache = 0;
/**
* 從緩存中取
*
* @param key
* @return
* @throws Exception
*/
synchronized public static Object getFromCacheFile(String key) {
String tempkey = key + "DATE"; // 為緩存的數據添加過期時間
Object obj = cacheFile.get(tempkey); // 獲得數據
if (obj == null) { // 判斷是否為空
return null;
}
Date d = new Date();
int time = d.compareTo(DateUtils.strToDateLong(String.valueOf(obj))); // 把保存的時間與現在手機上的時間對比
if (time >= 0) { // 如果超出,則刪除
removeValueByKey(key, tempkey);
return null;
} else {
return cacheFile.get(key);
}
}
/**
* 從緩存中移除數據
*
* @param key
*/
public static void removeValueByKey(String... key) {
long start = 0;
long end = 0;
// 先垃圾回收
System.gc();
start = Runtime.getRuntime().freeMemory();
for (int i = 0; i < key.length; i++) {
if (cacheFile.containsKey(key[i]))
cacheFile.remove(key[i]);
}
System.gc();
end = Runtime.getRuntime().freeMemory();
usedCache -= (end - start);
}
/**
* 保存數據島map
*
* @param key
* key
* @param obj
* value
* @param holdTime
* 保存時間
*/
public static void saveValueByKey(String key, Object obj, int holdTime) {
judgeContainer(); // 判斷容器是否超出限制大小
long start = 0;
long end = 0;
// 先垃圾回收
System.gc();
start = Runtime.getRuntime().freeMemory();
String subKey = "";
if (key.length() > 5) { // 由於為每一個key添加了一個key+DATE作為時間戳,所以這裡判斷是否存在這個時間戳
subKey = key.substring(key.length() - 5);
} else {
subKey = key;
}
if (subKey.contains("DATE")) {
if (cacheFile.containsKey(key)) {
removeValueByKey(key);
removeValueByKey(key.substring(0, key.length() - 4));
}
}
cacheFile.put(
key + "DATE",
DateUtils.getPreTime(DateUtils.getStringDate(),
String.valueOf(holdTime)));
cacheFile.put(key, obj);
System.gc();
end = Runtime.getRuntime().freeMemory();
usedCache += (end - start);
}
/**
* 判斷容器大小是否超出內存
*/
synchronized public static void judgeContainer() {
while (usedCache >= limit) {
cacheFile.remove(cacheFile.size() - 1);
}
}
/**
* 強制保存key value到map,如果之前存在則覆蓋
*
* @param key
* @param obj
* @param holdTime
*/
synchronized public static void saveMap2CacheFile(String key, Object obj,
int holdTime) {
judgeContainer();
// 首先放入時間的key給的key做關聯
saveValueByKey(key, obj, holdTime);
}
/**
* 放入緩存中,並判斷是否存在,不使用
*
* @param file
* @param holdTime
* @return
*/
synchronized public static void putMap2CacheFile(String key, Object obj,
int holdTime) {
judgeContainer();
saveValueByKey(key, obj, holdTime);
}
/**
* 放入緩存中,並判斷是否存在,不使用
*
* @param file
* @param holdTime
* @return
*/
synchronized public static void putMap2CacheFile(Map file,
int holdTime) {
judgeContainer();
for (String key : file.keySet()) {
saveValueByKey(key, file.get(key), holdTime);
}
}
}
Android Native層Binder.transact()函數調用 Binder.onTransact() 函數失敗分析
Q:Android Native層Binder.transact()函數調用 Binder.onTransact() 函數失敗?在Android Native層調用Cam
Android中利用viewflipper動畫切換屏幕效果
整個項目的package com.example.viewflipper;import android.R.integer;import android.app.Acti
Android 百度地圖 SDK v3.0.0 (一)
最近公司要把百度地圖集成的項目中,於是我就研究了一天百度地圖的SDK,當前的版本:Android SDK v3.0.0 。 雖然百度地圖網上相關代碼比較多,
WoWoViewPager動畫庫
目錄English READMEGradle備注Demo版本TodoLicense動畫用法基本動畫位移動畫縮放動畫漸現、漸逝動畫旋轉動畫文字大小動畫TextView Si