Question

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!

Was it helpful?

Solution

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

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).

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

OTHER TIPS

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.

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");
    }
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top