博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Saving Key-Value Sets
阅读量:4045 次
发布时间:2019-05-24

本文共 2932 字,大约阅读时间需要 9 分钟。

If you have a relatively small collection of key-values that you'd like to save, you should use the APIs. A object points to a file containing key-value pairs and provides simple methods to read and write them. Each file is managed by the framework and can be private or shared.

This class shows you how to use the APIs to store and retrieve simple values.

Note: The APIs are only for reading and writing key-value pairs and you should not confuse them with the APIs, which help you build a user interface for your app settings (although they use as their implementation to save the app settings). For information about using the APIs, see the guide.

Get a Handle to a SharedPreferences

You can create a new shared preference file or access an existing one by calling one of two methods:

  • — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any in your app.
  • — Use this from an if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.

For example, the following code is executed inside a . It accesses the shared preferences file that's identified by the resource string R.string.preference_file_key and opens it using the private mode so the file is accessible by only your app.

Context context = getActivity();SharedPreferences sharedPref = context.getSharedPreferences(        getString(R.string.preference_file_key), Context.MODE_PRIVATE);

When naming your shared preference files, you should use a name that's uniquely identifiable to your app, such as "com.example.myapp.PREFERENCE_FILE_KEY"

Alternatively, if you need just one shared preference file for your activity, you can use the method:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

Caution: If you create a shared preferences file with or , then any other apps that know the file identifier can access your data.

Write to Shared Preferences

To write to a shared preferences file, create a by calling on your .

Pass the keys and values you want to write with methods such as and . Then call to save the changes. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);SharedPreferences.Editor editor = sharedPref.edit();editor.putInt(getString(R.string.saved_high_score), newHighScore);editor.commit();

Read from Shared Preferences

To retrieve values from a shared preferences file, call methods such as and , providing the key for the value you want, and optionally a default value to return if the key isn't present. For example:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);long default = getResources().getInteger(R.string.saved_high_score_default));long highScore = sharedPref.getInt(getString(R.string.saved_high_score), default);

转载地址:http://jxgdi.baihongyu.com/

你可能感兴趣的文章
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>