Android本地化存储

自己项目中的使用的一个库,整理一下分享给大家
已经上传了github,欢迎大家start

用法

jitpack用起来比较方便

Step 1. Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
Step 2. Add the dependency

    dependencies {
            compile 'com.github.evernightking:Storage:v1.0'
    }

集成一个application ,在onCreate()方法中去初始化一下

public class MainApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Store.init(this,"testFile");
    }
}

第二个参数是保存本地的文件名

        Store.put("zhangsan", new TestBean());
        Store.put("string", "string");
        Store.put("int", 100);
        Store.put("long", 100L);
        Store.put("byte", (byte) 100);
        Store.put("short", (short) 100);
        Store.put("float", (float) 100);
        Store.put("boolean", true);
        ArrayList<TestBean> list = new ArrayList<>();
        list.add(new TestBean());
        list.add(new TestBean());
        list.add(new TestBean());
        Store.put("listsss", list);

        ArrayList<TestBean> listsss = Store.get("listsss", ArrayList.class, null);
        TestBean testBean = listsss.get(1);


        String s = Store.get("zhangsan", TestBean.class, null).toString() + "\n" +
                Store.get("string", "") + "\n"
                + Store.get("int", 0) + "\n" +
                Store.get("long", 0L) + "\n" +
                Store.get("byte", (byte) 0) + "\n"
                + Store.get("short", (short) 0) + "\n" +
                Store.get("float", (float) 0) + "\n" +
                Store.get("boolean", true) + "\n"
                + "\n" + testBean.toString();
读取出来的数据展示

文件中保存的数据

用base64简单加密了一下,使用还是很方便的,数据文件保存在data/data/包名/files/storage/文件夹中

代码很简单,就几个类,感兴趣的自己看一下吧

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容