package com.example.sharedpreferencestest;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SharedPreferencesTest extends Activity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preferences = getSharedPreferences("crazyit",MODE_PRIVATE);
editor = preferences.edit();
Button read = (Button)findViewById(R.id.read);
Button write = (Button)findViewById(R.id.write);
read.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//读取字符串
String time = preferences.getString("time",null);
//读取int类型数据
int randNum = preferences.getInt("random",0);
String result = time ==null ? "您暂时还未写入数据":
"写入时间为:"+time
+"\n上次生成的随机数为:"+randNum;
//使用Toast提示信息
Toast.makeText(SharedPreferencesTest.this,result,5000).show();
}
});
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
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();
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/read"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="read"/>
<Button
android:id="@+id/write"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="write"/>
</LinearLayout>
上面的程序示范了如何向SharedPreferences中写入、读取数据,该程序界面简单,提供两个按钮,其中一个用于写入数据,另一个用于读取数据。
读取SharedPreferences文件的时候如果文件根本不存在的时候,程序也返回默认值,不会抛出异常。
由于SharedPreferences并不支持写入Date类型的值,故程序使用SimpleDateFormate将Date格式化成字符串后写入。
SharedPreferences数据总是保存在/data/data/<package name >/shared_prefs 目录下,SharedPreferences数据总是以XML格式保存。通过File Explorer面板的到处文件按钮可以将文件导出。shared_prefs
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="random" value="76" />
<string name="time">2013年08月16日07:33:59</string>
</map>
本实例用到了
content.SharedPreferences;接口
SharedPreferences preferences;
preferences = getSharedPreferences("crazyit",MODE_PRIVATE);
通过Context提供的
public SharedPreferences getSharedPreferences (String name, int mode)
方法获取SharedPreferences实例 这个SharedPreferences是不能被外部程序访问的,名叫crazyit
content.SharedPreferences;内部接口Editor获取写入数据的Editor对象
SharedPreferences.Editor editor;
editor = preferences.edit();
使用preferences的edit()方法获取Editor对象
import java.text.SimpleDateFormat;
java.text包中的SimpleDateFormate类用来格式化当前时间
java.utilDate 的Date获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"+"hh:mm:ss");
//存入当前时间
editor.putString("time",sdf.format(new Date()));
这里用到了Editor对象的public abstract SharedPreferences.Editor putString (String key, String value)
方法
Math.random()方法
public static synchronized double random ()
Added in API level 1
Returns a pseudo-random double n, where n >= 0.0 && n < 1.0. This method reuses a single instance of Random. This method is thread-safe because access to the Random is synchronized, but this harms scalability. Applications may find a performance benefit from allocating a Random for each of their threads.
public abstract SharedPreferences.Editor putInt (String key, int value)