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使用的一個簡單總結。