Android、共享首选项还是内部存储?
我想存储一些基本数据,例如玩家名称、级别完成、最快速度、最快时间,并在每次玩家开始我正在制作的愚蠢小游戏时将其调出...这是最好的方法吗?
共享首选项还是内部存储?
我在 http://developer.android.com/guide/topics/数据/数据存储.html 并且刚刚对使用哪一个感到困惑,因为两者看起来都很好而且很容易做到。
建议?
谢谢!
I want to store some basic data like players_name,levels_completed,fastest_speed,fastest_time and bring it up each time the player starts a silly little game I am making... which is the perffered method to do this?
Sharedprefs or internal storage?
I am at http://developer.android.com/guide/topics/data/data-storage.html
and have just gotten confused as to which to use as both look good and easy enough to do.
Advise?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这几乎取自 Facebook sdks 示例之一(它允许您保存 FB 会话,因此用户不必每次都登录)...不过我会为您修改一下
}
我想您了解基本想法...
一些要点:我使用“KEY + 玩家名称”以防不同的玩家在同一部手机上玩(如果它是静态的,您会用另一个玩家的数据覆盖一个玩家的数据)。
此外,当您执行
pName = savingSession.getString(PLAYER_NAME,"NO_NAME!");
时,如果共享首选项中不存在任何内容,则默认值为“NO_NAME!”同样,对于程序中的 getInts (在本例中我将它们默认为 0),您可以执行
SessionStore.saveSession("Alex",50000,50000);
来保存会话等。希望这给出了如何使用它的一个很好的要点...同时请记住我是一个 Android 新手 - 这对我来说非常有用,但我不是专家:DThis is pretty much taken from one of the Facebook sdks examples (it allows you to save the FB session so the user doesn't have to login each time)... I'll modify it a bit for you though
}
I think you get the basic idea...
some points: I use "KEY + player_name" in case different players play on the same phone (if it was static you would overwrite the data of one player with anothers data).
Also, when you do
pName = savedSession.getString(PLAYER_NAME,"NO_NAME!");
if nothing exists in the shared preferences it defaults to the value "NO_NAME!" and likewise for the getInts (which in this case I have them default to 0)in the program you would do
SessionStore.saveSession("Alex",50000,50000);
to save the session, etc. Hope this gives a good gist of how to use it... Also keep in mind I'm an android newb - this works great for me but I'm no expert :D如果游戏数据是静态的,您可以使用共享首选项。如果是动态数据,例如玩家高分等,我会使用 sqlite 数据库。实际上,我认为数据库是一个更简单的选择,因为在内部存储上创建读/写缓冲区可能有点棘手。
If it is game data that is static you could use shared preferences. If it is dynamic data like player high scores etc I would use sqlite database. I actually think that a database is a simpler option as creating read / write buffers can be a bit tricky on internal storage.