讀寫數據至緩存文件的Android庫:CacheUtilsLibrary

jopen 9年前發布 | 18K 次閱讀 Android開發 移動開發 CacheUtilsLibrary

一個簡單的Android工具類庫用于將任意類型的數據寫進至緩存文件中,之后讀取它們。
使用Gson來序列化和反序列化這些數據。

Gradle

compile 'com.lifeofcoding:cacheutilslibrary:1.0.0@aar'

如果出現如下錯誤:

duplication file during packaging of APK ...
Path in archive: META-INF/LICENSE.txt
...

Please add such code in your android entry of your build.gradle file:
packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/MANIFEST.MF'
}

Configuration

You need to configureCacheUtilsLibraryin yourApplicationclass.

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // configure CacheUtilsLibrary
        CacheUtils.configureCache(this);
    }
}

Don't forget to declare theMyApplicationclass in yourAndroidManifest.xmlfile.

Usage

All sample code can be found in the MainActivity file.

CacheStringFile
// write
CacheUtils.writeFile(CACHE_FILE_STRING, CACHE_FILE_CONTENT_STRING);

// read
String fileContent = CacheUtils.readFile(CACHE_FILE_STRING);

CacheMap<String, T>File
// write
CacheUtils.writeDataMapFile(CACHE_FILE_MAP, getCacheFileContentMap());

// read
Map<String, Object> mapData = CacheUtils.readDataMapFile(CACHE_FILE_MAP);

// get mapData
private static Map<String, Object> getCacheFileContentMap() {
    Map<String, Object> mapData = new HashMap<>();
    mapData.put("firstItem", "item0");
    mapData.put("secondItem", 1);
    mapData.put("thirdItem", false);
    return mapData;
}

CacheList<Map<String, T>File
// write
CacheUtils.writeDataMapsFile(CACHE_FILE_LIST_MAP, getCacheFileContentListMap());

// read
List<Map<String, Object>> listMapData = CacheUtils.readDataMapsFile(CACHE_FILE_LIST_MAP);

// get listMapData
private static List<Map<String, Object>> getCacheFileContentListMap() {
    List<Map<String, Object>> listMapData = new ArrayList<Map<String, Object>>();

    for (int i = 0; i < 4; i++) {
        Map<String, Object> item = new HashMap<>();
        item.put("firstItemAt" + i, "item0At" + i);
        item.put("secondItemAt" + i, 1 + i);
        item.put("thirdItemAt" + i, i % 2 == 0);
        listMapData.add(item);
    }
    return listMapData;
}

CacheObjectFile
// write
CacheUtils.writeObjectFile(CACHE_FILE_OBJECT, MyClass.SAMPLE_MYCLASS_1);

// read
MyClass myClassSample = CacheUtils.readObjectFile(CACHE_FILE_OBJECT, new TypeToken<MyClass>(){}.getType());

You can seeMyClassandMyClass.SAMPLE_MYCLASS_1here and here.

CacheList<Object>File
// write
CacheUtils.writeObjectFile(CACHE_FILE_LIST_OBJECT, getCacheFileContentListObject());

// read
List<MyClass> myClassList = CacheUtils.readObjectFile(CACHE_FILE_LIST_OBJECT, new TypeToken<List<MyClass>>(){}.getType());

// get List<MyClass> data
private static List<MyClass> getCacheFileContentListObject() {
    List<MyClass> listObject = new ArrayList<>();
    listObject.add(MyClass.SAMPLE_MYCLASS_1);
    listObject.add(MyClass.SAMPLE_MYCLASS_2);
    listObject.add(MyClass.SAMPLE_MYCLASS_3);
    return listObject;
}

項目主頁:http://www.baiduhome.net/lib/view/home/1442134426888

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!