Android檔案儲存體--採用SharedPreferences儲存使用者喜好設定參數和讀取設定參數
Android SDK支援那些檔案儲存體技術?
1. 使用SharedPreferences儲存key-value類型的資料
2. 流檔案儲存體(使用openFileOutput和openFileInput方法,或FileInputStream和FileOutputStream)
3. XML半結構化儲存
4. 用JSON儲存數組和對象
5.用資料庫儲存結構化資料
6. 用第三方的物件導向資料庫直接儲存Java對象。
這篇博文主要介紹用SharedPreferences儲存key-value對的步驟和讀取設定參數的方法
1. 使用Context.getSharedPreferences方法擷取SharedPreferences對象,其中儲存key-value的檔案的名稱有getSharedPreferences方法第一個參數指定。
2. 使用SharedPreference.edit方法擷取SharedPreferences.Editor對象。
3. 通過SharedPreference.Editor介面的putXxx方法儲存key-value對。
4. 通過SharedPreference.Editor.commit方法提交要儲存的key-value對。
執行個體:SharedPreferences
MainActivity.java
[java]
package com.wwj.setting;
import java.util.Map;
import com.wwj.service.PreferencesService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText; //姓名框
private EditText ageText; //年齡框
private RadioGroup radioGroup; //單選框組
//商務邏輯類
private PreferencesService service;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText)findViewById(R.id.nameText);
ageText = (EditText)findViewById(R.id.ageText);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
service = new PreferencesService(this);
Map<String, String> params = service.getPerferences();
nameText.setText(params.get("name"));
ageText.setText(params.get("age"));
radioGroup.check(Integer.valueOf(params.get("sex"))); //設定選擇的選項按鈕
}
/**
* 在布局中按鈕控制項指定的onClick方法
* @param v
*/
public void save(View v) {
String name = nameText.getText().toString();
String age = ageText.getText().toString();
Integer sex = radioGroup.getCheckedRadioButtonId();
service.save(name, Integer.valueOf(age), sex);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}
}
package com.wwj.setting;
import java.util.Map;
import com.wwj.service.PreferencesService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText; //姓名框
private EditText ageText; //年齡框
private RadioGroup radioGroup; //單選框組
//商務邏輯類
private PreferencesService service;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameText = (EditText)findViewById(R.id.nameText);
ageText = (EditText)findViewById(R.id.ageText);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
service = new PreferencesService(this);
Map<String, String> params = service.getPerferences();
nameText.setText(params.get("name"));
ageText.setText(params.get("age"));
radioGroup.check(Integer.valueOf(params.get("sex"))); //設定選擇的選項按鈕
}
/**
* 在布局中按鈕控制項指定的onClick方法
* @param v
*/
public void save(View v) {
String name = nameText.getText().toString();
String age = ageText.getText().toString();
Integer sex = radioGroup.getCheckedRadioButtonId();
service.save(name, Integer.valueOf(age), sex);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}
}
PreferencesService.java
[java]
package com.wwj.service;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class PreferencesService {
private Context context;
public PreferencesService(Context context) {
this.context = context;
}
/**
* 儲存參數
* @param name 姓名
* @param age 年齡
* @param sex 性別
*/
public void save(String name, Integer age, Integer sex) {
//獲得SharedPreferences對象
SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", age);
editor.putInt("sex", sex);
editor.commit();
}
/**
* 擷取各項參數
* @return
*/
public Map<String, String> getPerferences() {
Map<String, String> params = new HashMap<String, String>();
SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
params.put("name", preferences.getString("name", ""));
params.put("age", String.valueOf(preferences.getInt("age", 0)));
params.put("sex", String.valueOf(preferences.getInt("sex", 0)));
return params;
}
}
package com.wwj.service;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class PreferencesService {
private Context context;
public PreferencesService(Context context) {
this.context = context;
}
/**
* 儲存參數
* @param name 姓名
* @param age 年齡
* @param sex 性別
*/
public void save(String name, Integer age, Integer sex) {
//獲得SharedPreferences對象
SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", age);
editor.putInt("sex", sex);
editor.commit();
}
/**
* 擷取各項參數
* @return
*/
public Map<String, String> getPerferences() {
Map<String, String> params = new HashMap<String, String>();
SharedPreferences preferences = context.getSharedPreferences("wwj", Context.MODE_PRIVATE);
params.put("name", preferences.getString("name", ""));
params.put("age", String.valueOf(preferences.getInt("age", 0)));
params.put("sex", String.valueOf(preferences.getInt("sex", 0)));
return params;
}
}
布局檔案activity_main.xml
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"/>
<EditText
android:id="@+id/nameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"/>
<EditText
android:id="@+id/ageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="性別" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
android:checked="true"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="@string/saveBtn" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"/>
<EditText
android:id="@+id/nameText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"/>
<EditText
android:id="@+id/ageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="性別" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
android:checked="true"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="save"
android:text="@string/saveBtn" />
</LinearLayout>