现在的位置: 首页 > 综合 > 正文

12—(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream、FileOutputStream将数据存储到SD卡中

2013年04月12日 ⁄ 综合 ⁄ 共 1777字 ⁄ 字号 评论关闭

 对于游戏中的数据进行保存方式,在Android中常用的有四种保存方式,这里我先给大家统一先简单的介绍下:

 

1.  SharedPreference

此保存方式试用于简单数据的保存,文如其名属于配置性质的保存,不适合数据比较大的保存方式;

 

2. 文件存储 (FIleInputStream/FileOutputStream)

此保存方式比较适合游戏的保存和使用,可以保存较大的数据,因为相对于SQLite来说更容易让童鞋们接受,此方式不仅能把数据存储在系统中也能将数据保存到SDcard中;

 

3.SQLite 

此保存方式比较适合游戏的保存和使用,可以保存较大的数据,并且可以将自己的数据存储到文件系统或者数据库当中,也可以将自己的数据存储到SQLite数据库当中,也能将数据保存到SDcard中;

 

4.ContentProvider (不推荐用于游戏保存)

此保存方式不推荐用于游戏保存,因为此方式不仅能存储较大数据,还支持多个程序之间就的数据进行交换!!! 但是由于游戏中基本就不可能去访问外部应用的数据,所以对于此方式我不予讲解, 有兴趣的可以去自行百度 google 学习;

 

以上简单的对几种常用的保存方式进行的概述,那么,下面会详细的去分析每个的优缺点以及每种保存的实现和需要注意的地方!

 

下面我首先向大家介绍第一种保存方式: 

 

             保存方式之:  《SharedPreference》


           优点: 简单、方便、适合简单数据的快速保存

           缺点:1.存数的文件只能在同一包内使用,不能在不同包之间使用!

            2.默认将数据存放在系统路径下 /data/data/com.himi/  ,没有找到放SD卡上的方法。

           总结:其实本保存方式如同它的名字一样是个配置保存,虽然方便,但只适合存储比较简单的数据!

 

main.xml  :

 

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <TextView android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content" android:text="保存数据练习!"  
  7.         android:textSize="20sp" android:textColor="#ff0000" android:id="@+id/tv_title" />  
  8.     <TextView android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" android:text="请输入帐号" />  
  10.     <EditText android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" android:id="@+id/editText_Login"  
  12.         android:text=""></EditText>  
  13.     <TextView android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content" android:text="请输入密码" />

抱歉!评论已关闭.