Android之本地数据存储(一):SharedPreferences

SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来存储一些轻量级的数据。

向SharedPreferences写入数据

1、使用getSharedPreferences(fileName, MODE_PRIVATE)获得SharedPreferences对象

2、通过SharedPreferences对象获得SharedPreferences.Editor对象

3、通过SharedPreferences.Editor以键值对的形式存储数据

4、调用SharedPreferences.Editor的commit()的提交存储的数据。

从SharedPreferences读取数据

1、使用getSharedPreferences(fileName, MODE_PRIVATE)获得SharedPreferences对象

2、使用sharedPreferences.getString("name", "")获得value的值 第二个参数是value的默认值

下面举例说明:

 

项目名称: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按钮,如下图:

猜你喜欢

转载自ch-kexin.iteye.com/blog/2326444