今天學習了SharedPreferences,經過老師的講解我們瞭解到SharedPreferences是Android平台上一個輕量級的儲存類,主要是儲存一些常用的配置比如視窗狀態,一般在Activity中 重載視窗狀態onSaveInstanceState儲存一般使用SharedPreferences完成,它提供了Android平台常規的Long長 整形、Int整形、String字串型的儲存。它分為多種許可權,可以全域共用訪問,android123提示最終是以xml方式來儲存,整體效率來看不是特別的高,對於常規的輕量級而言比SQLite要好不少,如果真的儲存量不大可以考慮自己定義檔案格式。xml 處理時Dalvik會通過內建底層的本地XML Parser解析,比如XMLpull方式,這樣對於記憶體資源佔用比較好。
這種方式應該是用起來最簡單的Android讀寫外部資料的方法了。他的用法基本上和 J2SE(java.util.prefs.Preferences)中的用法一樣,以一種簡單、 透明的方式來儲存一些使用者個人化的字型、顏色、位置等參數資訊。一般可以 通過Preferences來儲存,而程式員不需要知道它到底以什麼形式儲存的,儲存在了什麼地方。
在Android系統中,這些資訊以XML檔案的形式儲存在
/data/data/PACKAGE_NAME /shared_prefs 目錄下。
SharedPreferences pre = getSharedPreferences("soft",
Context.MODE_WORLD_READABLE);
在這裡我們可以調用 activity 為我們提供的方法,這個方法有兩個參數:
1). 檔案名稱 。 在這裡要特別注意 。 因為在 Android 中已經確定了 SharedPreferences 是以 xm l形式儲存,所以,在填寫檔案名稱參數時,不要給定 ” .xml ” 尾碼, android 會自動添加 。它是採用索引值對的形式儲存參數 。 當你需要獲得某個參數值時 , 按照參數的鍵索引即可。
2). 第二個可以理解為建立模式和之前的檔案儲存體的模式是一樣的。
Context. MODE_PRIVATE
Context. MODE_APPEND MODE_APPEND
Context. MODE_WORLD_READABLE
Context. MODE_WORLD_WRITEABLE
1、資源檔內容
<string name="name_text">姓名</string>
<string name="age_text">年齡</string>
<string name="save_text">提交</string>
2、布局檔案內容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="@string/name_text" />
<EditText
android:id="@+id/nameEt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="@string/age_text" />
<EditText
android:id="@+id/ageEt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/saveBtn"
android:layout_marginTop="30dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/save_text" />
</LinearLayout>
3、實現儲存
private void findViews() {
nameEt = (EditText) this.findViewById(R.id.nameEt);
ageEt = (EditText) this.findViewById(R.id.ageEt);
saveBtn = (Button) this.findViewById(R.id.saveBtn);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = nameEt.getText().toString().trim();
int age =
Integer.valueOf(ageEt.getText().toString().trim());
SharedPreferences sharedPreferences =
SharedpreferencesTestActivity.this
.getSharedPreferences("myOption", MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("name" ,name);
editor.putInt("age", age);
editor.commit();
}
});
}
效果
現在我們添加讀取功能:
1、資源檔內容:
<string name="name_text">姓名</string>
<string name="age_text">年齡</string>
<string name="save_text">儲存</string>
<string name="read_text">讀取</string>
2、布局檔案內容
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:stretchColumns="*">
<TableRow >
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/save_text" />
<Button
android:id="@+id/readBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/read_text" />
</TableRow>
</TableLayout>
3、onClick方法中的內容修改為:
public void onClick(View v) {
SharedPreferences sp = getSharedPreferences("myOption",
MODE_PRIVATE);
String name="無名氏";
int age = -1;
switch (v.getId()) {
case R.id.saveBtn:
name = nameEt.getText().toString().trim();
age = Integer.valueOf(ageEt.getText().toString().trim());
Editor editor = sp.edit();
editor.putString("name", name);
editor.putInt("age", age);
editor.commit();
break;
case R.id.readBtn:
name = sp.getString("name", "無名氏");
age = sp.getInt("age", -1);
Toast.makeText(this,
"姓名: "+name+ " 年齡: " +String.valueOf(age),
Toast.LENGTH_SHORT).show();
break;
}
}
效果
1、讀取其它應用程式的SharedPreferences
首先保證建立SharedPreferences的工程中使用的是:
Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE
2、下面我們看一個簡單的例子(讀取上面這個工程的SharedPreferences
):
資源檔內容:
<string name="app_name">ReadOtherSharedPreferences</string>
<string name="read_text">讀取其它工程的SharedPreferences檔案</string>
布局檔案內容:
<Button
android:id="@+id/readOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read_text" />
當前工程中讀取的代碼為:
public class ReadOtherSharedPreferencesActivity extends Activity implements OnClickListener {
Button readBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
}
private void findViews() {
readBtn = (Button) this.findViewById(R.id.readOther);
readBtn.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.readOther) {
try {
Context otherContext = this.createPackageContext(
"cn.class3g.activity", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences shared = otherContext.getSharedPreferences("info", MODE_PRIVATE);
String res = "name=" + shared.getString("name", "wms")+
" age="+ String.valueOf(shared.getInt("age", -1));
Toast.makeText(this, res, Toast.LENGTH_LONG).show();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
效果:
點擊Button
作者 chizhidan_luck