关于Android 持久层

    技术2026-10-02  6

    Android 持久层开发主要4个部分1 sharedperferences通过键值2元组存储简单数据2 文件存储3 内置的SQLite4 ContentProvide自定义抽象接口

    sharedperferences类似于hashtable, 不过它是存在android的文件系统里的,只要程序没有卸载这些程序存在取得sharedperferences用getSharedPreferences 方法官方文档如下public abstract SharedPreferences getSharedPreferences (String name, int mode)Retrieve and hold the contents of the preferences file 'name', returning a SharedPreferences through which you can retrieve and modify its values. Only one instance of the SharedPreferences object is returned to any callers for the same name, meaning they will see each other's edits as soon as they are made.第2个参数可以是下面的四个值    * MODE_PRIVATE    * MODE_WORLD_READABLE    * MODE_WORLD_WRITEABLE    * MODE_MULTI_PROCESS他们控制着访问SharedPreferences的权限sharedperferences支持int, float, long, string, boolean5个数据类型设置属性,可以参照如下例子:        Log.v("Debug", "SharedPreferenceStart");        SharedPreferences preferences = getSharedPreferences("TestFile", MODE_PRIVATE);        String  strTest = preferences.getString("TestKey", "NOValue");        Log.v("Debug", strTest);        if(strTest.equals("NOValue"))        {         SharedPreferences.Editor editor = preferences.edit();         editor.putString("TestKey", "TestValue"); // value to store         editor.commit();        }        strTest = preferences.getString("TestKey", "NOValue");        Log.v("Debug", strTest);        Log.v("Debug", "SharedPreferenceEnd");

    最新回复(0)