標籤:android sharepreferences sharepreferences的使用 資料存放區 android的shareprefere
不同於檔案的儲存方式,SharePreferences是使用鍵值對的方式來儲存資料的,也就是儲存資料的時候需要給這條資料提供一個對應的鍵,這樣在讀取資料的時候就可以通過這個鍵把相應的值取出來。而且SharePreferences支援多種不同的資料類型儲存,如果儲存的資料書整形類型,讀取出來的資料也是整形的,儲存的資料是一個字串,讀取出來的資料仍然是一個字串。
要想使用SharePreferences來儲存資料,首先要擷取到SharePreferences對象,android系統中主要提供了3種方式用於得到SharePreferences對象。
1.Context類中的getSharePreferences()方法:該方法接收2個參數,一個參數用於指定SharePreferences檔案的名稱,如果指定的檔案不存在則會建立一個,SharePreferences檔案都是存放在/data/data/<packagename>/shared_prefs/目錄下的。第二個參數用於指定操作模式,主要有2種模式可選,MODE_PRIVATE和MODE_MULTI_PROCESS。MODE_PRIVATE仍然是預設的操作模式,和直接傳入0效果是相同的,表示只有當前的應用程式才可以對這個SharePreferences檔案進行讀寫。MODE_MULTI_PROCESS則一般用於會有多個進程中對同一個SharePreferences檔案進行讀寫的情況。
2.Activity類中的getPreferences()方法:這個方法和Context中的getSharePreferences()方法很類似,不過只接收一個操作模式參數,因為使用這個方法時會自動將當前活動的類名作為SharePreferences檔案名稱。
3.PreferenceManager類中的getDefaultSharedPreferences()方法:這是一個靜態方法,它接收一個Context參數,並自動使用當前應用程式的包名作為首碼來命名SharePreferences檔案。
得到SharePreferences對象之後,就可以開始向SharePreferences檔案中儲存資料了,主要分為三部實現:
1.調用SharePreferences對象的edit()方法來擷取一個SharePreferences.Editor對象。
2.向SharePreferences.Editor對象中添加資料,比如添加一個布爾類型資料就使用putBoolean方法。
3.調用commit()方法將添加的資料提交,從而完成資料存放區操作。
將資料儲存到SharePreferences中
下面我們來個小例子測試下上面的內容吧,建立一個SharePreferencesTest項目,然後修改activity_main.xml中的代碼,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/save_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="save data" /></LinearLayout>
上面的代碼比較簡單,就不多說了,下面修改MainActivity類裡面的代碼,如下:
package com.jack.sharedpreferencestest;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button saveData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);saveData=(Button) findViewById(R.id.save_data);saveData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//獲得SharedPreferences.Editor對象SharedPreferences.Editor editor=getSharedPreferences("share",MODE_PRIVATE).edit();editor.putString("name", "jack");editor.putInt("age", 22);editor.putBoolean("married", false);editor.commit();//提交資料}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行程式,點擊save data按鈕,這時候的資料已經儲存了,我們藉助File Exploer來進行查看,進入到/data/data/com.jack.sharedpreferencestest/shared_prefs/目錄下,可以看到如下一個share檔案
把檔案匯出來,查看內容如下所示:
注意:有時候我們運行程式查看file explorer的時候,發現裡面沒有任何的檔案,這時候可能是由於我們沒在devices中選擇正確的啟動並執行進程,只要在下面的地方點擊當前運行程式的包名就行了,如下黑色線圈起來的地方:
這樣file explorer就會顯示目錄結構了。
從SharePreferences中讀取資料
從SharePreferences讀取資料比較的簡單,SharePreferences對象提供了一系列的get方法用於對儲存的資料進行讀取,每種get方法都對應SharePreferences.Editor中的一種put方法。這些get方法都接收兩個參數,第一個參數是鍵,傳入儲存資料時使用的鍵就可以得到相應的值了,第二個參數是預設值,即表示當傳入的鍵找不到相應的值時,會以什麼樣的預設值進行返回。
在上面的項目中進行修改,首先修改activity_main.xlm中的代碼,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/save_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="save data" /> <Button android:id="@+id/restore_data" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="restore data" /> </LinearLayout>
修改MainActivity中的代碼:
package com.jack.sharedpreferencestest;import android.app.Activity;import android.content.SharedPreferences;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button saveData;private Button restoreData;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);saveData=(Button) findViewById(R.id.save_data);restoreData=(Button) findViewById(R.id.restore_data);saveData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub//獲得SharedPreferences.Editor對象SharedPreferences.Editor editor=getSharedPreferences("share",MODE_PRIVATE).edit();editor.putString("name", "jack");editor.putInt("age", 22);editor.putBoolean("married", false);editor.commit();//提交資料}});restoreData.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSharedPreferences pref=getSharedPreferences("share", MODE_PRIVATE);String name=pref.getString("name", null);int age=pref.getInt("age", 0);boolean married=pref.getBoolean("married", false);Log.d("MainActivity", "name is "+name);Log.d("MainActivity", "age is "+age);Log.d("MainActivity", "married is "+married);}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
運行程式,點擊restore data按鈕,查看日誌如下所示:
上面的小例子就是讀取SharePreferences檔案裡面的內容的,可以舉一反三哦。
關於SharePreferences檔案的使用就總結到這,下面一篇我將總結下android資料SQLite的使用。
轉載請註明:http://blog.csdn.net/j903829182/article/details/40952301
android學習十(android的SharePreferences儲存)