Android存储数据有以下几种方式: 

1. 文件

2. SharedPreferences

3. 数据库

4. 网络

5. 内容提供者

 

下面是一些关于SharedPreferences的知识。

  SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常规的Long长 整形、Int整形、String字符串型的保存,它是什么样的处理方式呢?SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问,android123提示最 终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml 处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。

 

  这种方式应该是用起来最简单的Android读写外部数据的方法了。他的用法基本上和 J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,那么这些设置最后就可以 通过Preferences来保存,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。当然,如果你愿意保存其他的东西,也没有什么限制。只 是在性能上不知道会有什么问题。

  在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下。

下面是一个关于SharedPreferences的例子

用户界面:

layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name_label"
            android:layout_marginRight="10dp" />
        
        <EditText 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:id="@+id/name_edit" />
    </LinearLayout>
    
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/age_label"
            android:layout_marginRight="10dp" />
        
        <EditText 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:id="@+id/age_edit" />
    </LinearLayout>
    
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save_label"
            android:id="@+id/save_btn"
            android:layout_alignParentRight="true"
            android:onClick="save" />  <!-- 为按钮添加点击事件的另一种方式,必须在显示这个layout的activity中定义该方法,方法签名符合public void xxx(View view) -->
 </RelativeLayout> </LinearLayout>

MainActivity.java

......
public class MainActivity extends Activity {
    private EditText nameView;
    private EditText ageView;
    private SharedPreferenceService sharedPreferenceService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        nameView = (EditText) findViewById(R.id.name_edit);
        ageView = (EditText) findViewById(R.id.age_edit);
        sharedPreferenceService = new SharedPreferenceService();
        sharedPreferenceService.setContext(getApplicationContext());
        
        Map<String, String> result = sharedPreferenceService.getSharedPreferenceData();
        String name = result.get("name");
        String age = result.get("age");
        if(name != null) {
            nameView.setText(name);
        }
        if(age != null) {
            ageView.setText(age);
        }
    }
    
    public void save(View view) {
        String name = nameView.getText().toString();
        String age = ageView.getText().toString();
        sharedPreferenceService.saveSharedPreferenceData(name, Integer.valueOf(age));
    }

}
SharedPreferenceService.java
......
public class SharedPreferenceService {
    private Context context;

    public Map<String, String> getSharedPreferenceData() {
        Map<String, String> map = new HashMap<String, String>();
        SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        String name = sharedPreferences.getString("name", null);
        int age = sharedPreferences.getInt("age", Integer.MIN_VALUE);
        if(name != null) {
            map.put("name", name);
        }
        if(Integer.MIN_VALUE != age) {
            map.put("age", String.valueOf(age));
        }
        return map;
    }
    
    public void saveSharedPreferenceData(String name, Integer age) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putString("name", name);
        editor.putInt("age", age);
        editor.commit();
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }
    

}

 

当点击保存后,就会在

/data/data/com.lk.sharedpreferences/shared_prefs/userInfo.xml中。

 

posted on 2014-03-15 15:43  寒岁青松  阅读(88)  评论(0)    收藏  举报