【问题标题】:Is there any alternative of Sharedpreference in native android?在原生 android 中是否有任何替代 Sharedpreference 的方法?
【发布时间】:2017-03-01 01:59:59
【问题描述】:

我的要求是将数据持久存储在本机 android 可执行文件中。数据是原始类型。我可以在可执行文件中使用 SharedPreferences 吗?如果是,那么如何?如果否,那么是否有任何替代方法可以在本机端 android 中持久存储数据?

我不想在本机端或任何文件操作中使用 SQLite。

谢谢,

【问题讨论】:

    标签: android android-ndk sharedpreferences android-sharedpreferences persistent-storage


    【解决方案1】:

    编辑:对不起,我最初读错了问题

    我可以在可执行文件中使用 SharedPreferences 吗?

    是的。如果数据是原始类型,您绝对应该这样做。

    怎么做?

    来自安卓开发者documentation

    public class Calc extends Activity {
    public static final String PREFS_NAME = "MyPrefsFile";
    
    @Override
    protected void onCreate(Bundle state){
       super.onCreate(state);
       . . .
    
       // Restore preferences
       SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
       boolean silent = settings.getBoolean("silentMode", false);
       setSilent(silent);
    }
    
    @Override
    protected void onStop(){
       super.onStop();
    
      // We need an Editor object to make preference changes.
      // All objects are from android.context.Context
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putBoolean("silentMode", mSilentMode);
    
      // Commit the edits!
      editor.commit();
    }
    

    }

    我发布了关于如何使用 SharedPrefs here 的类似答案,请注意,SharedPreferences 的这个用例打破 HashMap 的好处,它非常快速地查找和获取值一把钥匙。

    您可以这样做的一种方法是:

    //if you are running the code inside from an Activity
    Context context = this;
    SharedPreferences userSharedPrefs = context.getSharedPreferences("USER_NAME_PREFS", MODE_PRIVATE);
    SharedPreferences pwdSharedPrefs = context.getSharedPreferences("PWD_PREFS", MODE_PRIVATE);
    

    getAll() 方法将返回一个名为HashMap 的数据结构,其工作方式类似于字典:

    对于存储的每个,都有一个唯一键

    旁注:一次性获取它们有点破坏了这个数据结构的目的,但让我们继续

        Map<String, String> userNameHashMap = (Map<String, String>)userSharedPrefs.getAll();
        Map<String, String> pwdHashMap = (Map<String, String>)pwdSharedPrefs.getAll();
    

    然后你可以对他们做任何你想做的事

    希望它们在列表中? (我假设你的用户名是字符串)

        List<String> userNameList = new LinkedList<>();
        userNameList.addAll(userNameHashMap.values());
    

    想知道用户 john 是否有密码?

        boolean johnHasPasswd = pwdHashMap.containsKey("john");
        String johnsPass;
    
        if(johnHasPasswd)
            johnsPass = pwdHashMap.get("john");
    

    如果你想使用原生的数据存储机制,你受限于以下

    您的数据存储选项如下:

    • Shared Preferences 将私有原始数据存储在键值对中。
    • Internal Storage 将私有数据存储在设备内存中。
    • External Storage 将公共数据存储在共享的外部存储上。
    • SQLite 数据库 将结构化数据存储在私有数据库中。
    • 网络连接 使用您自己的网络服务器在网络上存储数据

    你应该看看这些来自开发者网站的official docs

    希望这会有所帮助!

    【讨论】:

    • “仅基于您不想要 SharedPreferences 的要求” OP确实希望尽可能使用SharedPreferences
    • 感谢您的回复。我需要知道如何使用共享首选项将私有原始数据存储在 android NDK 中本机 C 中的键值对中。由于我正在处理二进制文件/可执行文件,因此不需要 JNI 实现。
    • @SudhirSinha 你能解决你的问题吗?
    • 我没有 JNIEnv 的实例,因为我正在处理独立的二进制文件。现在为了在 linux 中持久存储数据,我正在使用文件和使用 mmap 读取/写入文件。这解决了我的问题,并且由于mmap,文件操作也变得很快。
    • 您的回答似乎来自 android sdk 的观点,这是正确的,但我的问题与 android linux 中的本机端存储有关。
    【解决方案2】:

    共享偏好是存储先验数据的最佳解决方案:

        SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,         Context.MODE_PRIVATE);
        Editor editor = sharedpreferences.edit();
        editor.putString("key", "value");
        editor.commit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-30
      • 2023-03-27
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多