android - 保存播放器数据和状态

标签 android persistence

我做了一个 android 游戏,我想知道如何保存玩家数据,例如金钱、达到的等级等。我认为 XML 并不是一种真正方便的方法。有什么好办法吗? android有没有提供一些方法来保存数据?

最佳答案

对于您需要跟踪的数据类型,数据库可能有点过分了。我建议使用 Android 的 SharedPreferences API。 See here for a tutorial .

这是一个 official overview of Android data persistence方法包括 SharedPreferences 和 SQLite。

查看 getStringSet用于存储数组/集合的 API。

对于字符串集不存在的值:

Returns the preference values if they exist, or defValues. Throws ClassCastException if there is a preference with this name that is not a Set.

对于不存在的值数据,如string、bool、float等:

Returns the preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a String.

关于android - 保存播放器数据和状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5606152/

相关文章:

android - 1个月限制申请免费使用

android - 如何在靠近预定义位置时从 Android 发送短信 (SMS)

PHP - ini_set ('session.gc_maxlifetime' , 5) - 为什么它没有结束 session ?

spring - 从字符串转换为 Spring MVC 表单数据绑定(bind)的自定义对象?

database - Docker-compose v3 不保留 postgres 数据库

android - 收到 GCM 消息时如何启动应用程序而不是构建通知?

sql - 从 SQLite 中的 INSERT OR IGNORE 语句中查找自动递增的值

java - 初始化 reycler View 时出现 NullPointerException

apache-kafka - 卡夫卡流 : output to a topic first or persist directly?

io - Redis:请只保留不会过期的键?