游戏中简单数据存储
2015-11-28 13:21
232 查看
在游戏开发中,有时要设置持久的简单数据存储。
Preferences主要是使用简单,但是功能不是很强大,一般适合用于保存一些简单的用户设置的参数,是一种轻量级的存储机制。Preferences仅可以用来存储几种简单类型的数据,如:boolean、int、floate、long、或者String。这些数据以键值对的形式存储在应用程序私有的Preferences目录下的xml文件中。
Preferences主要是使用简单,但是功能不是很强大,一般适合用于保存一些简单的用户设置的参数,是一种轻量级的存储机制。Preferences仅可以用来存储几种简单类型的数据,如:boolean、int、floate、long、或者String。这些数据以键值对的形式存储在应用程序私有的Preferences目录下的xml文件中。
可以使用 SharedPreferences sp=this.getSharedPreferences("feiruo",Context.MODE_PRIVATE); 或者 SharedPreferences sp=PreferencesManager.getDefaultSharedPreferences(Context);
package com.mycompany.myapp; import android.app.*; import android.os.*; import android.view.*; import android.widget.*; import android.content.*; import java.util.*; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取SharedPreferences引用,存储名为feiruo.xml,读写模式为private //SharedPreferences sp=PreferencesManager.getDefaultSharedPreferences(this);
SharedPreferences sp=this.getSharedPreferences("feiruo",Context.MODE_PRIVATE); String lasttime=sp.getString("time",null); if(lasttime==null){ lasttime="你好,欢迎第一次光临"; }else{ lasttime="你好,上次登录时间为:"+lasttime; } SharedPreferences.Editor ed=sp.edit(); ed.putString("time",new Date().toLocaleString()); ed.commit();//提交修改; TextView tv=(TextView)this.findViewById(R.id.mainTextView); tv.setText(lasttime); } }
相关文章推荐
- 一个资深技术人的转型之路
- 基于网络传输的数据包的加密解密方法及终端
- jsp页面中的代码执行加载顺序介绍
- 阿里云服务器ECS添加数据盘
- Robots.txt 协议详解及使用说明
- [angular] 篇 指令中的scope
- UITableView 常用方法列表
- 浅析c与c++中struct的区别 http://www.jb51.net/article/39992.htm
- nodejs 像 C 语言那样输出当前代码的行数
- iOS开发中关于import头文件放置位置的tips
- 网络相关知识及设备解析
- c++ 深拷贝string类 简单实现
- ZOJ 2836 Number Puzzle(容斥原理)
- 二级菜单
- 常用UIButton控件的属性列表
- apache配置常用模块
- 注解Annotation 详解(转)
- 如何正确获取MYSQL的ADO连接字符串
- struct和typedef struct彻底明白了 http://www.cnblogs.com/qyaizs/articles/2039101.html
- 排序(希尔排序)