2010-02-28 傳智播客—Android(三) 數據存儲之二 SharedPreferences
Posted on 2010-03-01 15:01 長城 閱讀(2438) 評論(0) 編輯 收藏在此之前的學習內容是數據存儲之一文件存儲。在本地存儲中常用的有,文件、配置文件、數據庫。前面的學習主要是針對本地文件的。我認為可以把SharedPreferences看做是配置文件,雖然它也是采用XML格式存儲的。
比如我們使用的桌面軟件中,通常會有一個“選項”菜單,選項是對軟件的常規或核心設置。在Android中我們使用SharedPreferences來完成這種對配置文件的讀寫。在JavaSE和JavaEE中常用的是*.properties,在Windows平臺下常使用*.ini文件。
下面,我們編寫一個使用SharedPreferences讀寫配置文件的小例子。
1.創建Android工程
Project name:AndroidSharedPreferences
BuildTarget:Android2.1
Application name:Android 應用程序配置
Package name:com.changcheng.sharedpreferences
Create Activity:AndroidSharedPreferences
Min SDK Version:7
2.編輯strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, AndroidSharedPreferences!</string> <string name="app_name">Android 應用程序配置</string> <string name="tv_name">姓名</string> <string name="tv_age">年齡</string> <string name="bt_write">設置</string> <string name="bt_read">讀取</string> <string name="save_success">保存成功</string> <string name="save_failed">保存失敗</string> </resources> |
3.編輯main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 姓名 --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="70dip" android:layout_height="wrap_content" android:textSize="25dip" android:id="@+id/tv_name" android:text="@string/tv_name" /> <EditText android:layout_width="300dip" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_name" android:id="@+id/et_name" /> </RelativeLayout> <!-- 年齡 --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="70dip" android:layout_height="wrap_content" android:textSize="25dip" android:id="@+id/tv_age" android:text="@string/tv_age" /> <EditText android:layout_width="300dip" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_age" android:id="@+id/et_age" /> </RelativeLayout> <!-- 按鈕 --> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_write" android:id="@+id/bt_set" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/bt_set" android:text="@string/bt_read" android:id="@+id/et_read" /> </RelativeLayout> </LinearLayout> |
4.為按鈕添加事件代碼
package com.changcheng.sharedpreferences;
import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
public class AndroidSharedPreferences extends Activity {
private static final String TAG = "AndroidSharedPreferences"; private EditText etName; private EditText etAge;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 獲取按鈕 Button btSet = (Button) this.findViewById(R.id.bt_set); Button btRead = (Button) this.findViewById(R.id.bt_read); // 獲取編輯框 etName = (EditText) this.findViewById(R.id.et_name); etAge = (EditText) this.findViewById(R.id.et_age); // 添加事件 btSet.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 獲取名稱和年齡 String name = etName.getText().toString(); String age = etAge.getText().toString(); // 創建SharedPreferences SharedPreferences sp = getSharedPreferences("preferences", Context.MODE_PRIVATE); // 添加數據 Editor editor = sp.edit(); editor.putString("name", name); editor.putInt("age", Integer.parseInt(age)); // 保存數據 if (editor.commit()) Toast.makeText(AndroidSharedPreferences.this, R.string.save_success, 1).show(); else Toast.makeText(AndroidSharedPreferences.this, R.string.save_failed, 1).show(); } }); btRead.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 創建SharedPreferences SharedPreferences sp = getSharedPreferences("preferences", Context.MODE_PRIVATE); // 獲取數據 String name = sp.getString("name", "defName"); String age = sp.getInt("age", 0) + ""; // 顯示數據 etName.setText(name); etAge.setText(age); } }); } } |
5.運行程序
啟動模擬器,運行程序。輸入名稱和年齡,點擊保存。我們使用的代碼是getSharedPreferences("preferences",Context.MODE_PRIVATE);,當然commit時。它會為我們為”/data/data/com.changcheng.sharedpreferences/shared_prefs/preferences.xml”。將 preferences.xml導出,查看它的內容為:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">長城</string> <int name="age" value="25" /> </map> |
將名稱和年齡編輯框的內容清空,然后點擊讀取按鈕,剛才寫出的內容被讀取進來。 SharedPreferences的使用就是這么簡單。
6.其他程序訪問本程序的配置
通過SharedPreferences創建的配置文件,不需要指定路徑和文件后綴名,讀取的時候也是。通常情況下,配置只是提供給本應用程序使用的。在這里我們介紹一個小知識點,即其他程序想使用本應用程序的配置,那應該如何使用SharedPreferences呢?如下:
Context otherAppContext = createPackageContext("com.changcheng.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppContext.getSharedPreferences("preferences", Context.MODE_WORLD_READABLE);
注意,為了使其他程序可以訪問本應用程序的配置。那么在我們使用 getSharedPreferences創建配置的時候必須為它的文件訪問模式設置為允許其他程序讀取或寫入等。