简体   繁体   English

Android,共享偏好还是内部存储?

[英]Android, sharedpreferences or internal storage?

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? 我想存储一些基本数据,如players_name,levels_completed,faster_speed,faster_time,并在每次玩家开始我正在制作的愚蠢小游戏时启动它......这是执行此操作的常用方法吗?

Sharedprefs or internal storage? Sharedprefs还是内部存储?

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. 我在http://developer.android.com/guide/topics/data/data-storage.html并且对于使用哪个看起来很好且容易做到而感到困惑。

Advise? 建议?

Thanks! 谢谢!

This 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 这几乎取自其中一个Facebook sdks示例(它允许您保存FB会话,因此用户不必每次都登录)...我会稍微为您修改它

public class SessionStore {

private static final String PLAYER_NAME = "player_name";
private static final String LEVELS_COMPLETED = "levels_completed";
private static final String HIGH_SCORE = "high_score";

private static final String KEY = "player_session";

int highScore;
int levelsCompleted;
String pName;

public static boolean saveSession(Context context, String player_name, int levels_completed, int high_score) {
    Editor editor =
        context.getSharedPreferences(KEY + player_name, Context.MODE_PRIVATE).edit();
    editor.putString(PLAYER_NAME,player_name);
    editor.putInt(LEVELS_COMPLETED, levels_completed);
    editor.putInt(HIGH_SCORE,high_score);

    return editor.commit();
}

public static void restoreSession(Context context, String player_name) {
    SharedPreferences savedSession =
        context.getSharedPreferences(KEY + player_name, Context.MODE_PRIVATE);
    highScore = savedSession.getInt(HIGH_SCORE,0);
    levelsCompleted = savedSession.getInt(LEVELS_COMPLETED,0);
    pName = savedSession.getString(PLAYER_NAME,"NO_NAME!");

}

public String getName()
{
      return pName;
}

} }

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). 一些要点:我使用“KEY + player_name”以防不同的玩家在同一部手机上播放(如果它是静态的,你会用其他数据覆盖一个玩家的数据)。

Also, when you do pName = savedSession.getString(PLAYER_NAME,"NO_NAME!"); 此外,当你执行pName = savedSession.getString(PLAYER_NAME,"NO_NAME!"); if nothing exists in the shared preferences it defaults to the value "NO_NAME!" 如果共享首选项中不存在任何内容,则默认值为“NO_NAME!” and likewise for the getInts (which in this case I have them default to 0) 同样对于getInts(在这种情况下,我将它们默认为0)

in the program you would do SessionStore.saveSession("Alex",50000,50000); 在程序中你会做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 希望这给出了如何使用它的好主意......还要记住我是一个新手 - 这对我来说很有用但我不是专家:D

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. 如果它是像玩家高分等动态数据我会使用sqlite数据库。 I actually think that a database is a simpler option as creating read / write buffers can be a bit tricky on internal storage. 我实际上认为数据库是一个更简单的选项,因为在内部存储上创建读/写缓冲区可能有点棘手。

public void StoreTimeAppend(String MY_DATA, File file) {
    try {
        FileOutputStream fos = openFileOutput(String.valueOf(file), MODE_APPEND);
        Writer out = new OutputStreamWriter(fos, "UTF8");
        out.write(MY_DATA + '\n');
        out.close();
        fos.close();
    } catch (IOException e) {
        Tip.setText("ISSUE");
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM