saving data - Saving Key-Value Sets
来源:互联网 发布:淘宝商品广告违规 编辑:程序博客网 时间:2024/05/17 04:47
If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences
APIs. ASharedPreferences
object points to a file containing key-value pairs and provides simple methods to read and write them. EachSharedPreferences
file is managed by the framework and can be private or shared.
This class shows you how to use the SharedPreferences
APIs to store and retrieve simple values.
Note: The SharedPreferences
APIs are only for reading and writing key-value pairs and you should not confuse them with the Preference
APIs, which help you build a user interface for your app settings (although they use SharedPreferences
as their implementation to save the app settings). For information about using the Preference
APIs, see the Settings 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:
getSharedPreferences()
— Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from anyContext
in your app.getPreferences()
— Use this from anActivity
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 Fragment
. 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 getPreferences()
method:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Caution: If you create a shared preferences file with MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
, 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 SharedPreferences.Editor
by calling edit()
on yourSharedPreferences
.
Pass the keys and values you want to write with methods such as putInt()
and putString()
. Then callcommit()
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 getInt()
and getString()
, 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);int defaultValue = getResources().getInteger(R.string.saved_high_score_default);long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
- saving data - Saving Key-Value Sets
- Saving Data --- Saving Key-Value Sets(一)
- Saving Key-Value Sets
- Saving Key-Value Sets保存到Preference
- Saving Key-Value Sets 官方文档翻译
- 12Saving Key-Value Sets(SharedPreferences的使用)
- saving data - Saving Files
- : saving data
- Saving Data
- Saving Data
- Saving Data
- Saving Data --- Saving Files(二)
- saving data - Saving Data in SQL Databases
- Android Training ---Saving data
- android-Saving Data
- AndroidTraining学习------Saving-Data
- Saving Data笔记
- Saving Data in iOS
- android4.4如何开机横屏
- rpm打包原理学习
- NSDictionary的遍历
- 单链表就地逆置(Java版),单链java版
- iOS开发中使用宏定义提高开发效率
- saving data - Saving Key-Value Sets
- 缓存学习点滴
- java 压缩解压文件 无乱码
- 在WebView中如何让JS与Java安全地互相调用
- ServiceStack.Redis 之 IRedisTypedClient<第四篇>
- linux多线程
- Universal Windows App Development with Cortana and the Speech SDK 5
- ServiceStack.Redis常用操作 - 事务、并发锁
- Material Theme