Preferences存储数据,需要有Context才能取到,单独的一个context是不容易取到的,但是,Activity继承自Context,所以,Preferences数据的存储需要有Activity来支持,sp(SharePreferences的简写)需要从context中得到,Editor需要从sp中得到。其中,sp是读取数据的关键,editor是存储数据的关键。所以,我们存取数据的时候,首先要知道是在哪个Activity中进行的操作才行。
Preferences能够读取int,long,boolean,String,float这几种类型的数据。很简单,但是,已经能够满足我们对简单的游戏数据的存储了。游戏中一些常用的玩家信息,关卡信息,都可以存储。
这里实现了一个简单的数据存储的框架,这里游戏中的信息都存储在同一个文件中,便于在不同的Activity中操作,处理的是相同的数据(当然,你也可以在不同的文件中操作,这样做的前提是你的游戏够复杂,你处理信息的条理够清楚)。
我们先看下基本的Profile类:
ackage com.example.sample2_3;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class Profile{
/*
* Profile在游戏中作为一个单例存在
* */
public static String s_fileName = "Profile";
public static Profile s_instance = null;
public static Profile getInstance()
{
if (s_instance == null) {
s_instance = new Profile();
}
return s_instance ;
}
/*
* 游戏中存储的一些信息
* */
public String m_playerName = "player_name";
public int m_playerID = 0 ;
public int m_socre = 0 ;
public boolean m_isFirstEnter = false ;
public SharedPreferences m_pPreferences = null ;
public SharedPreferences.Editor m_editor = null ;
private Profile()
{
}
/*
* 初始化preferences和editor,至于Activity,在哪个Activity中
* 都是一样的,只要我们的文件名字没有变,我们得到的文件都是同一个文件。
* 所以,放心的初始化吧!使用之前都要进行初始化,如果,我们切换了Activity
* 记得在新的Activity中重新初始化。
* */
public void init(Activity activity)
{
m_pPreferences = activity.getSharedPreferences(s_fileName, Context.MODE_PRIVATE);
m_editor = m_pPreferences.edit();
}
/*
* 信息的存储
* */
public void save()
{
m_editor.putBoolean("is_first_enter", m_isFirstEnter);
m_editor.putInt("player_id", m_playerID);
m_editor.putString("player_name", m_playerName);
m_editor.putInt("score", m_socre);
m_editor.commit();
}
/*
* 信息的加载
* */
public void load()
{
m_isFirstEnter = m_pPreferences.getBoolean("is_first_enter", true);
m_playerName = m_pPreferences.getString("player_name", "player_name");
m_playerID = m_pPreferences.getInt("player_id", -1);
m_socre = m_pPreferences.getInt("score", 0);
}
public String getM_playerName() {
return m_playerName;
}
public void setM_playerName(String m_playerName) {
this.m_playerName = m_playerName;
}
public int getM_playerID() {
return m_playerID;
}
public void setM_playerID(int m_playerID) {
this.m_playerID = m_playerID;
}
public int getM_socre() {
return m_socre;
}
public void setM_socre(int m_socre) {
this.m_socre = m_socre;
}
public boolean isM_isFirstEnter() {
return m_isFirstEnter;
}
public void setM_isFirstEnter(boolean m_isFirstEnter) {
this.m_isFirstEnter = m_isFirstEnter;
}
}
解析:
这里将Profile作为一个单例来处理,方便我们在整个游戏过程中对于数据的处理。
s_fileName:xml的名字,上面已经说过了,这样做是为了处理方便,理论上一个文件足够我们处理简单的游戏了。
public void init(Activity activity):初始化SharePreferences,我们读取数据需要SharePreferences和editor,所以,在每个需要的Activity中都应该对其进行初始化。
public void save():保存数据,这里是进行的统一的处理,也就是说,我们可能只改变了一个值,但是,save的时候要把所有的值都保存一遍,这样处理起来确实效率有点低,但是,实际开发中我们并不是每时每刻都在保存数据的,当我们的数据多到这样保存影响运行效率的时候,我想,那个时候我们应该考虑使用数据库来保存数据。而不是用这样的方式了。
public void load():数据的加载,一般情况下,我们第一次进入游戏的时候要进行数据的加载,也就是初始化Profile中的数据,让他们得到正确的值。
set方法:基本上每个数据都有它的set方法:我的建议是每次调用set方法后都要进行save操作,保证我们的数据更新到了xml中,当然,你也可以优化,去掉save方法,在set方法中添加commit操作。
get方法:得到当前的数据,要知道我们的profile是一个单例,就算是你切换了Activity,那么,你get到的值也是你最新set的值(即使你set后没有save或者commit).
package com.example.sample2_3;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
/*
* 初始化
* */
Profile.getInstance().init(this);
/*
* 加载
* */
Profile.getInstance().load();
TextView tv01 = (TextView)this.findViewById(R.id.TextView02);
tv01.setText(Profile.getInstance().getM_playerName());
/*
* 重置数据
* */
Profile.getInstance().setM_isFirstEnter(false);
Profile.getInstance().setM_playerID(101123);
Profile.getInstance().setM_playerName("AndroidPalyer");
Profile.getInstance().setM_socre(500);
/**
* 最重要的,重置后要保存,否则,再次读取的时候无效
* */
Profile.getInstance().save();
TextView tv02 = (TextView)this.findViewById(R.id.TextView03);
tv02.setText(Profile.getInstance().getM_playerName());
}
}
解析:
上面的代码是对Profile的一个使用,有初始化,显示,重置数据,再显示。
Activity_XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Preferences操作"
android:textSize="18dip"
android:gravity="center"
/>
<TextView
android:text=""
android:id="@+id/TextView02"
android:textColor="#00ff00"
android:background="#ffffff"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text=""
android:id="@+id/TextView03"
android:textColor="#00ff00"
android:background="#ffffff"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
以上就是对Proferences使用的一个简单总结。