標籤:android sharedpreferences
很多時候我們開發的軟體需要向使用者提供軟體參數設定功能,例如我們常用的QQ,使用者可以設定是否允許陌生人添加自己為好友。對於軟體配置參數的儲存,如果是window軟體通常我們會採用ini檔案進行儲存,如果是j2se應用,我們會採用properties屬性檔案或者xml進行儲存。如果是Android應用,我們最適合採用什麼方式儲存軟體配置參數呢?Android平台給我們提供了一個SharedPreferences類,它是一個輕量級的儲存類,特別適合用於儲存軟體配置參數。使用SharedPreferences儲存資料,其背後是用xml檔案存放資料,檔案存放在/data/data/<package name>/shared_prefs目錄下。
下面用預設路徑的設定,舉例說明其用法,首先是布局檔案:
<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/pathName" /><Buttonandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/btn_savePath" /><TextView android:id="@+id/explaition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pathset_explation" /></LinearLayout>
下面是PreferencesService類:
package lhjd.cnc.dispenser.setting;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 * @throws Exception */public void save(String name) throws Exception{SharedPreferences preferences=context.getSharedPreferences("pathSet", Context.MODE_WORLD_READABLE);Editor editor=preferences.edit();editor.putString("name", name);editor.commit();}/** * 實現應用參數提取 * @return * @throws Exception */public Map<String, String> getPreferences() throws Exception{SharedPreferences preferences=context.getSharedPreferences("pathSet", Context.MODE_WORLD_READABLE);Map<String, String> result=new HashMap<String, String>();result.put("name", preferences.getString("name", ""));return result;}}
下面是activity
public class PathSet extends Activity{private EditText pathNameText;private PreferencesService service;public PathSet(final Context context) {super(context);LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.pathset, this);service=new PreferencesService(context);pathNameText=(EditText)this.findViewById(R.id.pathName); Map<String, String> params = null;try {params = service.getPreferences();} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}pathNameText.setText(params.get("name")); Button button=(Button)findViewById(R.id.btn_savePath); button.setOnClickListener(new View.OnClickListener(){public void onClick(View v){String name=pathNameText.getText().toString().trim();if("".equals(name)||name==null){Toast.makeText(context, "路徑不可為空", 1).show();}else{try{service.save(name);Toast.makeText(context, R.string.succss, 1).show();} catch (Exception e){Toast.makeText(context, R.string.fail, 1).show();e.printStackTrace();}}}}); }}
下面是效果:
Android 使用SharedPreferences進行資料存放區和讀取資料