java - 使用共享首选项保存永久数据?

标签 java android

我正在使用共享首选项为我的游戏制作高分脚本。显然,我希望这些数据仅保存在安装它的手机上。我已完成以下操作,并且高分显示并更新,但是当我关闭应用程序并重新打开它时,它会重置。

            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             = 0;

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,
    HighScore = 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 ;
}
}

最佳答案

最好不要在 sharedpreference 中存储敏感信息。所有拥有 root 权限的用户都可以查看并轻松编辑您的共享首选项(如果未加密)。因此,不要忘记加密所有数据。

这是您需要存储在sharedpreference中的代码块

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("finalScore", yourscore);
editor.commit();

更新

您正在设置为

editor.putInt("HighScore", HighScore);

并得到

HighScore = settings.getInt("Age",0);

您应该使用相同的标签

更新

改变

HighScore = settings.getInt("HighScore", HighScore);

settings.getInt("HighScore", HighScore);

关于java - 使用共享首选项保存永久数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24109971/

相关文章:

java - 测试基于注解的 RequestInterceptor

java - 共享元素转换 Activity

Android gps以编程方式打开和关闭

Java GUI,需要使用 Action 监听器吗?

Java 泛型 : Unbounded wildcard is not working with Object type argument

java - 如何在不同的 Activity 中使用相同的 ArrayList(Android 和 Java)

android - 如何在 DBFlow 中进行复杂查询?

android - 如何配置 Acralyzer?

java - Pyjnius 自定义 java 方法返回 'JavaException: Unable to find a None Method' 在 Public Static 之后工作

java - Eclipse 调试器在包行处停止