本篇来介绍一下Android数据存储的第二中方式:SharedPreferences,这种方式在保存软件的各项参数时应用最广泛,一般采用.xml文件来进行存放。
实现的简单界面如下:
在Activity的子类中,我们可以通过两种方式来获取
第一种是:
getPreferences(MODE_PRIVATE); //这种方式默认采用所在的类名作为xml文件的名称
第二种是:
getSharedPreferences("preferencesFile", Context.MODE_PRIVATE); //我们可以指定参数保存文件的文件名称
本篇中采用第二种方式在service对象中指定保存的文件名称。
工程目录结构:
源代码:
main.xml
- <?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" >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/username" />
- <EditText
- android:id="@+id/name"
- android:layout_width="fill_parent"
- android:singleLine="true"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/age" />
- <EditText
- android:id="@+id/age"
- android:layout_width="fill_parent"
- android:numeric="integer"
- android:singleLine="true"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/save"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/save"
- android:onClick="save"/>
-
- </LinearLayout>
Strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, MainActivity!</string>
- <string name="app_name">软件参数设置</string>
- <string name="username">姓名</string>
- <string name="age">年龄</string>
- <string name="save">保存参数</string>
- </resources>
MainActivity.java
- package com.preferences.activity;
-
- import java.util.Map;
-
- import com.preferences.service.PreferencesService;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- private EditText name;
- private EditText age;
- private PreferencesService service;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- name = (EditText)findViewById(R.id.name);
- age = (EditText)findViewById(R.id.age);
- service = new PreferencesService(MainActivity.this);
- //打开时读取保存的参数
- Map<String,String> params = service.getPreferences();
- name.setText(params.get("username"));
- age.setText(params.get("age"));
- }
- public void save(View v){
- String userName = name.getText().toString();
- int userAge = Integer.parseInt(age.getText().toString());
- service.save(userName, userAge);
- Toast.makeText(MainActivity.this, "保存成功", 1).show();
- }
- }
PreferencesService.java
- package com.preferences.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) {
- super();
- this.context = context;
- }
-
- /**
- * 保存参数
- * @param name 姓名
- * @param age 年龄
- */
- public void save(String name,int age){
- SharedPreferences sharedPreferences = context.getSharedPreferences("itcastPreference", Context.MODE_PRIVATE);
- Editor editor = sharedPreferences.edit();
- editor.putString("username", name);
- editor.putInt("age", age); //目前是保存在内存中,还没有保存到文件中
- editor.commit(); //数据提交到xml文件中
- }
-
- /**
- * 获取各项配置参数
- * @return params
- */
- public Map<String,String> getPreferences(){
- SharedPreferences sharedPreferences = context.getSharedPreferences("itcastPreference", Context.MODE_PRIVATE);
- Map<String,String> params = new HashMap<String, String>();
- params.put("username", sharedPreferences.getString("username", ""));
- params.put("age", String.valueOf(sharedPreferences.getInt("age", 0)));
- return params;
- }
- }
当我们点击保存参数按钮之后,数据会保存到 /data/data/ [packagename] /shared_prefs目录下:
地址: http://download.csdn.net/category