Android平台给我们提供了一个SharedPreferences类,它是一个轻量级应用程序内部轻量级的存储方案,特别适合用于保存软件配置参数,比如boolean,int,float,long,String等数据.使用SharedPreferences保存数据,其实质是采用了xml文件存放数据,路径为:/data/data/<package name>/shared_prefs.
获取SharedPreferences的两种方式:
1 调用Context对象的getSharedPreferences()方法
2 调用Activity对象的getPreferences()方法
两种方式的区别:
调用Context对象的getSharedPreferences()方法获得的SharedPreferences对象可以被同一应用程序下的其他组件共享.
调用Activity对象的getPreferences()方法获得的SharedPreferences对象只能在该Activity中使用.
SharedPreferences的四种操作模式:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件.
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件.
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取.
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入.
案例
1写第二个界面 设置一张背景图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher"
android:orientation="vertical" >
</LinearLayout>
2写第二个界面的Activity (new class 继承Activity 重写onCreate方法)
package com.example.sharedpreference;
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setContentView(R.layout.second);
super.onCreate(savedInstanceState);
}
}
3在清单文件配置第二个界面
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sharedpreference"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.sharedpreference.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.sharedpreference.Second" >
</activity>
</application>
</manifest>
4编写主界面
package com.example.sharedpreference;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.R.bool;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
public class MainActivity extends Activity {
private static final String PREFS_NAME = "txtname";
Handler handler = new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
Intent intent = new Intent(MainActivity.this,Second.class);
startActivity(intent);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取偏好设置文件中的值,首次调用此文件不存在
SharedPreferences settings = getSharedPreferences(PREFS_NAME,0);
//参数1键值 参数2默认值
//第一个参数是保存时候的键值,第二个参数是默认值,当找不到偏好设置文件的
//时候采用此默认值
boolean isFirst =settings .getBoolean("isFirst", true);
if (isFirst) {
//第一次打开,给个导航说明
Message message = new Message();
message.what = 1;
//延迟5s后发送过去
handler.sendMessageDelayed(message, 5000);
}else {
//not第一次,直接跳转首页
Intent intent = new Intent(MainActivity.this,Second.class);
startActivity(intent);
}
}
//保存第一次使用状态
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//偏好设置:存储简单数据
//参数 文件名 + 访问模式
SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
//Editor 用于修改文件内容
SharedPreferences.Editor editor = settings.edit();
//
editor.putBoolean("isFirst", false);
//提交修改 才会创建文件 否则不会创建 类似数据库的事件回滚机制
//存储目录 :/data/data/包名/shared_prefs/文件名称.xml
editor.commit();
}
}
程序第一次运行时 会出现helloWord 5s后跳转到机器人界面
导出/data/data/包名/shared_prefs/文件名称.xml如下
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<map> <boolean value="false" name="isFirst"/> </map>