Android中5大数据存储(一)---SharedPreferences存储

本文介绍了在Android开发中如何使用SharedPreferences来保存和加载应用的配置信息,包括字符串、整型、布尔型等基本数据类型的应用场景及操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在实际的软件运行中,往往需要许多配置参数信息,如Windows操作系统的引导文件boot.ini就保存了操作系统的配置参数,在编写JavaSE或JavaEE时,也往往会使用资源文件(*.properties)保存一些系统的而配置信息,而在Android中,如果想要实现配置信息的保存则需要使用SharedPreferences完成。

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

下面举例说明:

项目名称:ShareData

以下是文件说明:

SavaData.java用来实现文件的存储,activity_save.xml为其布局文件,当点击保存文件按钮时,信息保存在share.xml

LoadData.java用来从share.xml文件读取数据,并将内容显示,activity_load.xml为布局文件

注意:当点击显示数据按钮会从SaveActivity--->跳入LoadActivity(使用Intent实现,前面文章有介绍)

下面依次是布局贴图和源码:

activity_save.xml源码:

<RelativeLayout 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: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="cn.lh.share.SaveActivity" >

    <Button
        android:id="@+id/savebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/savebtn" />
    <Button
        android:id="@+id/showbtn"
        android:layout_toRightOf="@id/savebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/showbtn" />
    

</RelativeLayout>

SaveData.java源码:


package cn.lh.share;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class SaveActivity extends Activity {
	
	private Button showbtn = null;
	private Button savebtn = null;
	//定义文件名称
	private static final String FILENAME = "share";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_save);
        showbtn = (Button)findViewById(R.id.showbtn);
        showbtn.setOnClickListener(new showListener());
        savebtn = (Button)findViewById(R.id.savebtn);
        savebtn.setOnClickListener(new productListener());
    }

    public class productListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			//指定操作文件的名称
			SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE);
			//编辑文件
			Editor edit = share.edit();
			//保存内容
			edit.putString("author", "LuHua");
			edit.putInt("age", 26);
			//更新文件
			edit.commit();
		
		}
    	
    }
    
    public class showListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(SaveActivity.this, LoadActivity.class);
			startActivity(intent);
		}
    	
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.save, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_load.xml源码:

<RelativeLayout 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: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="cn.lh.share.SaveActivity" >

    <TextView
        android:id="@+id/authorinfo"
        android:textSize="22sp"
        android:textColor="#ff00ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/ageinfo"
        android:layout_below="@id/authorinfo"
        android:textSize="22sp"
        android:textColor="#ffff00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

LoadData.java源码:

package cn.lh.share;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class LoadActivity extends Activity {

	private static final String FILENAME = "share";
	private TextView authorinfo = null;
	private TextView ageinfo = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_load);
		authorinfo = (TextView)findViewById(R.id.authorinfo);
		ageinfo = (TextView)findViewById(R.id.ageinfo);
		//指定操作文件名称
		SharedPreferences share = getSharedPreferences(FILENAME, MODE_PRIVATE);
		authorinfo.setText("作者:"+share.getString("author", "没有作者信息"));
		ageinfo.setText("年龄:"+share.getInt("age", 0));
	}

}

最后说下生成的share.xml文件保存的位置

文件被保存在DDMS中,可以选择Window->Open Perspective->Other命令,打开DDMS

打开之后选择 File Explorer\data\data\<package name>\shared_prefs,如下图:

也可以将生成的文件导出,首先选中share.xml文件,再单击DDMS工具栏中的Pull a file from the device按钮,如下图:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值