Android数据存储(一)---SharedPreferences

Android数据的存储方式:
Android系统一共提供了四种数据存储方式。分别是:SharePreference、SQLite、Content Provider和File;此外还有一种网络存储。由于Android系统中,数据基本都是私有的,都是存放于“data/data/程序包名”目录下,所以要实现数据共享,正确方式是使用Content Provider。


SharedPreferences:
在所有应用程序中,都必然涉及数据的交互。有些时候,应用程序有少量的数据需要保存,并且这些数据的格式很简单。SharedPreferences类供开发人员保存和获取基本数据类型的键值对.该类主要用于基本类型, 例如boolean、float、int、long和string。适用范围,比如:软件设置、用户账户设置,用户习惯设置等,这个时候就可以用到SharedPreferences。


其实,SharedPreferences使用xml格式为Android应用提供一种永久的数据存贮方式,并且是使用键值对的方式来存储数据的。对于一个Android应用,它存贮在文件系统的/data/data/包名/shared_prefs/目录下,可以被处在同一个应用中的所有Activity 访问(全局的)。Android 提提供了相关的API来处理这些数据而不需要程序员直接操作这些文件或者考虑数据同步的问题。

存储位置如图:


存储形式:



因为SharedPreferences本身是一个接口,程序无法直接创建SharedPreferences的实例,只能通过Context提供的getSharedPreferences(String name,int mode)方法来获取SharedPreferences的实例:

public abstract SharedPreferences getSharedPreferences(String name,int mode)


此方法接收两个参数,第一个参数用于指定SharedPreferences文件的名称(格式为xml文件),如果指定的文件不存在则会创建一个。SharedPreferences文件都是存放在/data/data/包名/shared_prefs/目录下的;第二个参数用于指定操作模式:

MODE_PRIVATE:默认操作模式,和直接传0效果相同,表示只有当前应用程序才可以对这个SharedPreferences文件进行读写
MODE_WORLD_READABLE:指定此SharedPreferences对其他程序只读且无法修改。
MODE_WORLD_WRITEABLE:指定此SharedPreferences能被其他程序读写。
MODE_MULTI_PROCESS:Android2.3之后已经弃之不用了。


得到SharedPreferences对象后,就可以向SharedPreferences文件中存储数据了,主要分为以下三步:
1.调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象
2.向SharedPreferences.Editor对象中添加数据,比如添加一个布尔型数据就使用putBoolean方法,添加一个字符串就用putString()方法,以此类推

3.调用commit()方法将添加的数据提交,从而完成数据存储操作

这里只是简单的演示存取boolean、int和String,其他的都是相似的:

activity_mian.xml布局文件,简单布局:

<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"
    tools:context="com.itman.sharedpreferencesdemo.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="保存" />

        <Button
            android:id="@+id/save_boolean"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="boolean" />

        <Button
            android:id="@+id/save_int"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="int" />

        <Button
            android:id="@+id/save_string"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="string" />
    </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="获取" />

        <Button
            android:id="@+id/get_boolean"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="boolean" />

        <Button
            android:id="@+id/get_int"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="int" />

        <Button
            android:id="@+id/get_string"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="string" />
    </LinearLayout>

</LinearLayout>

MainActivity.java程序:

public class MainActivity extends ActionBarActivity implements OnClickListener {
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;

	private Button save_boolean;
	private Button save_int;
	private Button save_string;

	private Button get_boolean;
	private Button get_int;
	private Button get_string;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitView();
		
		//实例化一个sharedPreferences对象
		sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE);
	}

	private void InitView() {
		save_boolean = (Button) findViewById(R.id.save_boolean);
		save_int = (Button) findViewById(R.id.save_int);
		save_string = (Button) findViewById(R.id.save_string);
		get_boolean = (Button) findViewById(R.id.get_boolean);
		get_int = (Button) findViewById(R.id.get_int);
		get_string = (Button) findViewById(R.id.get_string);

		save_boolean.setOnClickListener(this);
		save_int.setOnClickListener(this);
		save_string.setOnClickListener(this);
		get_boolean.setOnClickListener(this);
		get_int.setOnClickListener(this);
		get_string.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save_boolean:
			//需要改变sharedPreferences的值就必须获取编辑器
			editor = sharedPreferences.edit();
			editor.putBoolean("boolean_value", true);
			editor.commit();
			
			break;
		case R.id.save_int:
			//需要改变sharedPreferences的值就必须获取编辑器
			editor = sharedPreferences.edit();
			editor.putInt("int_value", 10);
			editor.commit();
			
			break;
		case R.id.save_string:
			//需要改变sharedPreferences的值就必须获取编辑器
			editor = sharedPreferences.edit();
			editor.putString("string_value", "Layne_Yao");
			editor.commit();
			
			break;
		case R.id.get_boolean:
			//如果只是获取sharedPreferences里面的值,就不用editor
			boolean bool = sharedPreferences.getBoolean("boolean_value", false);
			Toast.makeText(this, "您刚刚存储的boolean值:"+bool, Toast.LENGTH_SHORT).show();
			break;
		case R.id.get_int:
			//如果只是获取sharedPreferences里面的值,就不用editor
			int value = sharedPreferences.getInt("int_value", 0);
			Toast.makeText(this, "您刚刚存储的int值:"+value, Toast.LENGTH_SHORT).show();
			break;
		case R.id.get_string:
			//如果只是获取sharedPreferences里面的值,就不用editor
			String str = sharedPreferences.getString("string_value", "null");
			Toast.makeText(this, "您刚刚存储的string值:"+str, Toast.LENGTH_SHORT).show();
			break;

		}

	}

}

运行结果:(没有保存之前获取,如左图,获取默认值)

             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值