(libgdx学习)Preferences

官方文档的部分解释:

Preferences are a simple way to store small data for your application, e.g. user settings, small game state saves and so on. Preferences work like a hash map, using strings as keys, and various primitive types as values. Preferences are also the only way to date to write persistent data when your application is run in the browser.

Preferences非常适合用于为你的应用存储一些数据量较小的数据。它类似于一个HashMap。是一种key-value的形式


On Android, the system's SharedPreferences class is used. This means preferences will survive app updates, but are deleted when the app is uninstalled.

在android平台下,Android系统的SharedPreferences将会被使用(这也是为什么libgdx中的Preferences的使用会跟Android原生系统中的Preferences的用法那么像)。update的时候,preferenceszhogn的数据会保存,但是当删除app的时候后,其所对应的数据也会被删除。


1、初始化及常用的API


Preferences prefs = Gdx.app.getPreferences("My Preferences");//获取名为My preferences的Preferences
		
		prefs.putString("name", "Donald Duck");//往Preferences里面插数据
		prefs.flush();//将数据冲刷进去(重要)
		String name = prefs.getString("name","no name stored");//获取key为name的数据,默认值是no name stored
		
		prefs.putBoolean("soundOn", true);
		prefs.putInteger("highscore", 10);


二、应用举例

package com.example.groupactiontest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.GL10;

public class MyGame implements ApplicationListener {

	
	@Override
	public void create() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");//获取名为My preferences的Preferences
		
		prefs.putString("name", "Donald Duck");//往Preferences里面插数据
		prefs.flush();//将数据冲刷进去(重要)
		String name = prefs.getString("name","no name stored");//获取key为name的数据,默认值是no name stored
		
		System.out.println("--------->name:" + name);
		
		prefs.putBoolean("soundOn", true);
		prefs.putInteger("highscore", 10);
		
		prefs.flush();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

}


四、源码下载

http://download.csdn.net/detail/caihongshijie6/7039945



你可能感兴趣的:((libgdx学习)Preferences)