一、SharedPreferances简介
应用程序需要较小的数据量需要保存并且格式很简单,例如配置信息,是否打开音乐啊,震动等等。对于
这样的数据,android提供了ShardPreferences用来保存。
二、SharedPreferences特性
其保存的数据主要是类似于配置信息格式的数据,保存的数据主要是简单类型的key-value对。
SharedPreferences接口本身没有提供写入数据的能力,而是通过他的内部接口,SharedPreferences调用edit()方法既可获得他的Editor对象。具体会在下面示例程序中写出。
SharedPreference本事是一个接口,程序无法直接创建其实例,只能通过context提供的getSharedPreferances(String name,int mode)方法来获取实例。
其中的第二个参数值如下:
Context.MODE_PRIVATE:指定该数据只能被本应用程序读写。
Context.MODE_WORLD_READBLE:数据能被其他程序读取,但不能写。
Context.MODE_WORLD_WRITEABLE:数据能被其他程序读写。
三、示例程序
第二次打开程序,按下write再按下read按钮后:
布局文件:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
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=".MainActivity" >
<Button
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="162dp"
android:layout_y="28dp"
android:text="write" />
<Button
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="54dp"
android:layout_y="28dp"
android:text="read" />
<EditText
android:id="@+id/num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="58dp"
android:layout_y="145dp"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_x="101dp"
android:layout_y="123dp"
android:text="被访问次数:"
android:textSize="20dp" />
</AbsoluteLayout>
程序文件:
package com.example.sharedpreferences;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取只能被本程序读写的sharedpreferance对象
preferences=getSharedPreferences("test",MODE_WORLD_READABLE);
editor =preferences.edit();
Button read=(Button)findViewById(R.id.read);
Button write=(Button)findViewById(R.id.write);
EditText num=(EditText)findViewById(R.id.num);
//读取count数据
int count=preferences.getInt("count", 0);
//显示被访问的次数
num.setText("程序被使用了"+count+"次");
editor.putInt("count", ++count);
editor.commit();
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//读取字符串数据
String time=preferences.getString("time", null);
//读取int类型的数据
int random=preferences.getInt("random", 0);
String result=time==null?"您没有写入数据":"写入时间为:"+time+"\n上次生成的随机数为:"+random;
//使用toast提示信息
Toast.makeText(MainActivity.this, result, 5000).show();
}
});
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
//存入当天时间
editor.putString("time", sdf.format(new Date()));
//存入一个随机数
editor.putInt("random", (int)(Math.random()*100));
//提交数据
editor.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
其中用getSharedPreferences获取SharePreferences对象。editor是获取的Editor对象。
getxxx(String key,xxx defvalue)方法是获取key指定的value,如果key不存在,返回默认值defvalue,其中xxx可以是String,int,float等基本类型。
程序使用了SimpleDateFormat将Date格式化成字符串后写入。
写入数据后,可以用DDMS打开文件浏览,找到保存的数据文件: