我正在使用共享偏好来存储我的玩家的高分。高分功能有效,高分可以保存,但是当我对代码进行更改时,玩家的高分将重置为0.我很担心这个,因为如果我将来需要更新,它将重新启动我的玩家分数。
public void highscoreSaver() {
if(finalScore > SharedPrefManager.getHighScore()) {
SharedPrefManager.SetHighScore(finalScore);
SharedPrefManager.StoreToPref();
}
}
SharedPrefManager.java:
package com.divergent.thumbler;
import android.content.Context;
import android.content.SharedPreferences;
// all methods are static , so we can call from any where in the code
//all member variables are private, so that we can save load with our own fun only
public class SharedPrefManager {
//this is your shared preference file name, in which we will save data
public static final String MY_EMP_PREFS = "MySharedPref";
//saving the context, so that we can call all
//shared pref methods from non activity classes.
//because getSharedPreferences required the context.
//but in activity class we can call without this context
private static Context mContext;
// will get user input in below variables, then will store in to shared pref
private static int HighScore;
public static void Init(Context context)
{
mContext = context;
}
public static void LoadFromPref()
{
SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0);
// Note here the 2nd parameter 0 is the default parameter for private access,
//Operating mode. Use 0 or MODE_PRIVATE for the default operation,
settings.getInt("HighScore", HighScore);
}
public static void StoreToPref()
{
// get the existing preference file
SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0);
//need an editor to edit and save values
SharedPreferences.Editor editor = settings.edit();
editor.putInt("HighScore", HighScore); // Age is the key and mAge is holding the value
//final step to commit (save)the changes in to the shared pref
editor.commit();
}
public static void DeleteSingleEntryFromPref(String keyName)
{
SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0);
//need an editor to edit and save values
SharedPreferences.Editor editor = settings.edit();
editor.remove(keyName);
editor.commit();
}
public static void DeleteAllEntriesFromPref()
{
SharedPreferences settings = mContext.getSharedPreferences(MY_EMP_PREFS, 0);
//need an editor to edit and save values
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
}
public static void SetHighScore(int score)
{
HighScore = score;
}
public static int getHighScore()
{
return HighScore ;
}
}
答案 0 :(得分:0)
您永远不会将HighScore
整数设置为值。我想也许你认为这条线正在做一些与实际不同的事情:
settings.getInt("HighScore", HighScore);
这将调用getInt函数,该函数返回在第一个参数中具有键的值。如果该值不存在于共享首选项中,则会返回HighScore。您可能想要的是:
HighScore = settings.getInt("HighScore", 0)
这会将HighScore设置为" HighScore"下的设置中的值。如果没有值,那么它将返回0.
如果您打算致电LoadFromPref()
,请务必致电getHighScore()
!
答案 1 :(得分:0)
SharedPreferences用于存储应用程序级别值。每当您对代码进行更改并再次运行应用程序时,它都会创建共享首选项作为新文件&以前的所有数据都会重置
如果您在进行一些代码更改并重新运行应用程序后仍希望得分。最好将分数存储在文件或数据库中,如果设备中已存在,则避免创建文件或副本数据库。因此,您始终可以随时获取该数据。您也可以使用内容提供商。
答案 2 :(得分:0)
这是我为共享的prefrences写的一个类。它在我正在进行的简单游戏中运行良好。您只需在活动中使用此单例,您需要设置/获取游戏的高分。我将它用于默认的共享优先级,但如果需要,它也可以用于多个优先级。
/**
*
* This class is used for reading / writing data to a single prefrences
* file.
*
* Simply use setHighScore and getHighScore to get the score from a
* single prefrences file
*
*/
public enum SharedPrefrencesUtil {
UTIL;
private final String HIGH_SCORE = "HighScore";
/**
* Gets a single shared prefrences file
*/
public SharedPreferences getDefaultSharedPreference(Activity activity) {
return activity.getPreferences(Context.MODE_PRIVATE);
}
/**
* Gets a specific shared preferences file if there are multiples in the application
*/
public SharedPreferences getSharedPreferences(String preferenceFileName,
Context context) {
return context.getSharedPreferences(preferenceFileName, Context.MODE_PRIVATE);
}
/**
* Sets a score into a the default prefrences file
*/
public void setScore(Activity activity, int score){
activity.getPreferences(Context.MODE_PRIVATE).edit().putInt(HIGH_SCORE, score).commit();
}
/**
* Gets the score from the default prefrences file
*/
public int getHighScore(Activity activity) {
return activity.getPreferences(Context.MODE_PRIVATE).getInt(HIGH_SCORE, 0);
}
}