個人安卓學習筆記---重要的SharedPreferes
SharedPreferes
SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態,一般在Activity中 重載窗口狀態onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平臺常規的Long長 整形、Int整形、String字符串型的保存,它是什麼樣的處理方式呢?SharedPreferences類似過去Windows系統上的ini配置文件,但是它分爲多種權限,可以全局共享訪問,android123提示最 終是以xml方式來保存,整體效率來看不是特別的高,對於常規的輕量級而言比SQLite要好不少,如果真的存儲量不大可以考慮自己定義文件格式。xml 處理時Dalvik會通過自帶底層的本地XML Parser解析,比如XMLpull方式,這樣對於內存資源佔用比較好。
這種方式應該是用起來最簡單的Android讀寫外部數據的方法了。他的用法;’存的,保存在了什麼地方。當然,如果你願意保存其他的東西,也沒有什麼限制。只是在性能上不知道會有什麼問題。
在Android系統中,這些信息以XML文件的形式保存在
/data/data/PACKAGE_NAME /shared_prefs 目錄下。
SharedPreferencespre = 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.寫資源文件strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="name_str">姓名</string>
- <string name="age_str">年齡</string>
- <string name="save_str">保存</string>
- <string name="get_str">讀取</string>
- </resources>
2.寫佈局文件main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/name_str" />
- <EditText
- android:id="@+id/nameEt"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:text="@string/age_str"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"/>
- <EditText
- android:layout_height="wrap_content"
- android:layout_width="match_parent"
- android:id="@+id/ageEt"/>
- <TableLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="0,1">
- <TableRow >
- <Button
- android:id="@+id/saveBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/save_str"/>
- <Button
- android:id="@+id/getBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/get_str"/>
- </TableRow>
- </TableLayout>
- </LinearLayout>
3.實現保存和讀取
- package song.activity;
- /*利用SharePreferences創建和讀取文件*/
- import android.app.Activity;
- 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 SharePreferencesActivity extends Activity implements
- OnClickListener {
- //定義組建
- Button saveBtn, getBtn;
- EditText nameEt, ageEt;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- findViews();
- }
- //添加組件
- private void findViews() {
- nameEt = (EditText) this.findViewById(R.id.nameEt);
- ageEt = (EditText) this.findViewById(R.id.ageEt);
- saveBtn = (Button) this.findViewById(R.id.saveBtn);
- getBtn = (Button) this.findViewById(R.id.getBtn);
- saveBtn.setOnClickListener(this);
- getBtn.setOnClickListener(this);
- }
- //覆蓋onclick()方法
- public void onClick(View v) {
- SharedPreferences shared = this.getSharedPreferences("info", MODE_PRIVATE);
- String name = "無名氏";
- int age = -1;
- switch (v.getId()) {
- //保存按鈕
- case R.id.saveBtn:
- name = nameEt.getText().toString();
- age = Integer.valueOf(ageEt.getText().toString().trim());
- // 編輯器
- Editor editor = shared.edit();
- editor.putString("name", name);
- editor.putInt("age", age);
- // 寫入到設備中,保證操作事物的完整性
- editor.commit();
- break;
- //讀取按鈕
- case R.id.getBtn:
- name = shared.getString("name", "無名氏");
- age = shared.getInt("age", -1);
- Toast.makeText(this, "姓名: " + name + " 年齡: " + String.valueOf(age),
- Toast.LENGTH_SHORT).show();
- break;
- }
- }
- }
當運行程序後,點擊保存後,我們來看一下程序有沒有保存在xml中了呢?
4. 讀取其它應用程序的SharedPreferences
- Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE
- try {
- Context otherContext =
- this.createPackageContext("cn.class3g.activity",
- Context.CONTEXT_IGNORE_SECURITY);
- SharedPreferences sp =
- otherContext.getSharedPreferences("myOption",
- MODE_PRIVATE);
- String res = "姓名:"+ sp.getString("name", null).toString()
- + " 年齡:"+ String.valueOf(sp.getInt("age", -1));
- Toast.makeText(this, res, Toast.LENGTH_LONG).show();
- } catch (NameNotFoundException e) {
- e.printStackTrace();
- }
注意:如果兩個工程的package相同的話,即使創建是採用的私有模式也可以在另一個工程中順利讀取