目錄
SharedPreferences是Android中的數據存儲技術,常用來存儲一些輕量級的數據。
實際上SharedPreferences是 NoSQL 資料庫,用於處理的key-value鍵值對存儲,類似Windows 系統的註冊表,Linux 系統裡的 Berkeley DB (bdb) 以及衍生出的 dba,mdb 這類 hash 表資料庫。
Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容 Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就創建新檔案. Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有權限讀寫該檔案. MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取. MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入.
Button buttonPut = (Button) findViewById(R.id.buttonPut); buttonPut.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //實例化SharedPreferences對象 SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); //實例化SharedPreferences.Editor對象 SharedPreferences.Editor editor = sharedPreferences.edit(); //用putString的方法保存數據 editor.putString("name", "Neo"); editor.putString("nickname", "netkiller"); editor.putBoolean("sex", true); editor.putInt("age", 30); editor.putFloat("tall", 180.23f); Set<String> books = new HashSet<String>(); books.add("Netkiller Linux 手札"); books.add("Netkiller Java 手札"); books.add("Netkiller Android 手札"); editor.putStringSet("books", books); //提交當前數據 editor.commit(); } });
Button buttonGet = (Button) findViewById(R.id.buttonGet); buttonGet.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //實例化SharedPreferences對象 SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); //使用getString方法獲得value, String name = sharedPreferences.getString("name", ""); String nickname = sharedPreferences.getString("nickname", ""); boolean sex = sharedPreferences.getBoolean("sex", false); int age = sharedPreferences.getInt("age", 0); float tall = sharedPreferences.getFloat("tall", 0f); Set<String> books = sharedPreferences.getStringSet("books", null); Log.i("SharedPreferences", String.format("%s,%s,%s,%s,%s,%s", name, nickname, sex, age, tall, books.toString())); } });
SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); if (sharedPreferences.contains("nickname")) { Log.i("SharedPreferences", sharedPreferences.getString("nickname", "")); }else{ Log.i("SharedPreferences", "key: nickname 不存在"); }
SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.remove("nickname"); editor.commit(); Log.i("SharedPreferences", sharedPreferences.getAll().toString());
SharedPreferences sharedPreferences = getSharedPreferences("test", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.clear(); editor.commit(); Log.i("SharedPreferences", sharedPreferences.getAll().toString());
SharedPreferences 的數據存儲再一個 xml 檔案中,他的地址是:
//<package name>應替換成應用的包名, <name> File xmlFile = new File("/data/data/<package name>/shared_prefs/<name>.xml");
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">陳景峰</string> <string name="nickname">netkiller</string> <int name="age" value="30" /> </map>