问题描述
我使用的共享preferences来存储我的球员排行榜。高分功能的工作原理,以及高分扑救,但是当我更改了我的code,球员高分被重置为0。我很担心这一点,因为如果我有在未来的更新将重新启动我的球员分数。
公共无效highscoreSaver(){ 如果(finalScore>共享prefManager.getHighScore()){
共享prefManager.SetHighScore(finalScore);
共享prefManager.StoreTo preF();
}}
共享prefManager.java:
包com.divergent.thumbler;进口android.content.Context;
进口android.content.Shared preferences;//所有方法都是静态的,所以我们可以从code任何地方拨打
//所有成员变量都是私有的,这样我们就可以节省负载与我们自己的唯一的乐趣
公共类共享$ P $ {pfManager
//这是你的共享preference文件名,在此我们将保存数据
公共静态最后弦乐MY_EMP_ preFS =MyShared preF;//节能的背景下,这样我们就可以调用所有
//共享preF方法从非活动课。
//因为getShared preferences所需的上下文。
//但在活动课,我们可以不这样的背景下调用
私有静态语境mContext;//将获得以下变量用户输入,然后将存储共享preF
私有静态诠释高分;公共静态无效初始化(上下文的背景下)
{
mContext =背景;
}
公共静态无效LoadFrom preF()
{
共享preferences设置= mContext.getShared preferences(MY_EMP_ preFS,0);
//注意这里的第二个参数0是私接的默认参数,
//操作模式。使用0或MODE_PRIVATE默认操作,
settings.getInt(高分,高分);}
公共静态无效StoreTo preF()
{
//获取现有preference文件
共享preferences设置= mContext.getShared preferences(MY_EMP_ preFS,0);
//需要一个编辑器编辑和保存价值
共享preferences.Editor编辑= settings.edit(); editor.putInt(高分,高分); //年龄是关键,法师持有的价值 //最后一步犯(保存)更改共享preF
editor.commit();
}公共静态无效DeleteSingleEntryFrom preF(字符串注册表项目)
{
共享preferences设置= mContext.getShared preferences(MY_EMP_ preFS,0);
//需要一个编辑器编辑和保存价值
共享preferences.Editor编辑= settings.edit();
editor.remove(注册表项目);
editor.commit();
}公共静态无效DeleteAllEntriesFrom preF()
{
共享preferences设置= mContext.getShared preferences(MY_EMP_ preFS,0);
//需要一个编辑器编辑和保存价值
共享preferences.Editor编辑= settings.edit();
editor.clear();
editor.commit();
}
公共静态无效SetHighScore(INT分)
{
高分=得分;
}公共静态INT getHighScore()
{
返回高分;
}
}
下面是一类我写的共享prefrences。它在一个简单的游戏我工作的正常工作。在这里你需要设置/获取游戏的高分,你可以简单地使用这个单在您的活动。我将其用于默认共享prefrences,但如果需要,它也可用于多个prefrences
/ **
*
*这个类是用于读取/写入数据到单一prefrences
*文件。
*
*只需使用setHighScore和getHighScore从获得的分数
*单prefrences文件
*
* /
公共枚举共享$ P $ {pfrencesUtilUTIL;私人最终字符串HIGH_SCORE =高分;/ **
*获取一个单一的共享prefrences文件
* /
公众共享preferences getDefaultShared preference(活动活动){
返回activity.get preferences(Context.MODE_PRIVATE);
}/ **
*获取是否存在于应用程序的倍数的特定共享preferences文件
* /
公众共享preferences getShared preferences(字符串preferenceFileName,
上下文的背景下){ 返回context.getShared preferences(preferenceFileName,Context.MODE_PRIVATE);}/ **
*设置一个比分变成了默认的prefrences文件
* /
公共无效setScore(活动活动,诠释得分){
activity.get preferences(Context.MODE_PRIVATE).edit()putInt(HIGH_SCORE,评分).commit()。
}/ **
*获取从默认的prefrences文件比分
* /
公众诠释getHighScore(活动活动){
返回activity.get preferences(Context.MODE_PRIVATE).getInt(HIGH_SCORE,0);}}
I am using sharedpreferences to store my players highscore. The highscore function works, and the highscore saves, but when I make a change to my code, the players highscore is reset to 0. I am worried about this because if I have to update in the future it will restart my players scores.
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 ;
}
}
Here is a class I wrote for shared prefrences. It works fine in a simple game I am working on. You can simply use this singleton in your activity where you need to set/fetch the games high score. I use it for default shared prefrences, but it can also be used for multiple prefrences if needed.
/**
*
* 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);
}
}
这篇关于共享preferences不保存,当我编译我的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!