Android开发之【数据存储】SharedPreferences

对于数据的存储操作而言,Android之中一共提供了五种方式:SharedPreferences方式、文件存储方式、SQLite数据存储、Content Provider方式、网络存储。

SharedPreferences提供了一些基础的信息保存功能。所有的信息都是按照“key=value”的形式进行保存的,但是android.content.SharedPreferences接口所保存的信息只能是一些基本的数据类型,例如:字符串、整型、布尔型等。

在讲解SharedPreferences存储方式之前,首先需要复习一下Java中的Properties类,此类的属性的操作类,而且在属性文件之中只能保存基本的数据类型,而且属性文件更多的情况下是作为配置文件出现的,像Struts中的ApplicationResponseProperties文件就是一个资源文件。

支持数据类型:boolean、int、float、long、String。

Android之中直接操作,这种操作都需要Activity程序类的支持才可以。

本程序由于此时只关注操作方法的使用,所以不做过多的显示界面。

范例: SharedPreferencesProject(保存资源信息)

View Code
package org.lxh.demo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class MySharedPreferencesDemo extends Activity {
    // 保存的文件名称
    private static final String FILENAME = "mldn"; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
       // 指定要操作的文件名称
       SharedPreferences shared =
            super.getSharedPreferences(FILENAME, MODE_PRIVATE);
       // 编辑文件
       SharedPreferences.Editor editor = shared.edit();
       // 保存信息
       editor.putString("txtname", "刘德华");
       editor.putString("txtage", "55");
       edit.commit();// 提交更新
    }
}    

注:数据保存必须使用commit(),才能真正保存信息。

范例:  SharedPreferencesProject(读取资源文件)

View Code
<?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:id="@+id/authorinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textSize="22px"
        android:textColor="#FFFFFF" />
    <TextView 
        android:id="@+id/ageinfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:textSize="22px"
        android:textColor="#FFFFFF" />
</LinearLayout>
View Code
package org.lxh.demo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class MySharedPreferencesDemo extends Activity {
    private static final String FILENAME = "mldn"; // 保存的文件名称
    private TextView authorinfo = null ;
    private TextView ageinfo = null ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        this.authorinfo = (TextView)
                  super.findViewById(R.id.authorinfo) ;
        this.ageinfo = (TextView) super.findViewById(R.id.ageinfo) ;
        SharedPreferences share = 
              super.getSharedPreferences(FILENAME,
                Activity.MODE_PRIVATE);
        this.authorinfo.setText("作者:" + share.getString("author", 
            "没有作者信息。")) ;
        this.ageinfo.setText("年龄:" + share.getInt("age", 0)) ;
    }     
} 

在读取数据时,可直接利用getXxx()根据Key进行读取,

也可以直接通过getAll()将全部的数据按照Map集合集合的方式取出。

如果没有对应的Key,则会将默认值设置到文本组件中。

 

所有读取进来的数据都在组件中直接显示。

需要特别说明的是,以上的代码只是显示资源文件的操作流程,而实际上更多情况下,资源文件可以用于保存配置信息。例如:你正在看小说,希望关上之后下次可以继续打开之前所关闭的进度点,那么在这种情况下就可以利用此方法进行保存。

 

原文地址:https://www.cnblogs.com/androidsj/p/3074650.html