Using Shared Preferences
来源:互联网 发布:arttemplate.js官网 编辑:程序博客网 时间:2024/06/05 18:48
SharedPreferences
类类似windows下的ini文件,用于存储一些配置信息。
The SharedPreferences
class provides a general framework that allows youto save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences
to save any primitive data: booleans, floats, ints, longs, andstrings. This data will persist across user sessions (even if your application is killed).
User Preferences
Shared preferences are not strictly for saving "user preferences," such as what ringtone auser has chosen. If you're interested in creating user preferences for your application, see PreferenceActivity
, which provides an Activity framework for you to createuser preferences, which will be automatically persisted (using shared preferences).
To get a SharedPreferences
object for your application, use one oftwo methods:
getSharedPreferences()
- Use this if you need multiple preferences files identified by name,which you specify with the first parameter.getPreferences()
- Use this if you needonly one preferences file for your Activity. Because this will be the only preferences filefor your Activity, you don't supply a name.
To write values:
- Call
edit()
to get aSharedPreferences.Editor
. - Add values with methods such as
putBoolean()
andputString()
. - Commit the new values with
commit()
To read values, use SharedPreferences
methods such as getBoolean()
and getString()
.
Here is an example that saves a preference for silent keypress mode in acalculator:
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,MODE_PRIVATE);//提供文件名,可设置读写权限 // getPreferences(MODE_PRIVATE); //不提供文件参数,默认为工程名。在工程的私有文件夹的share_prefs可找到相应的文件 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,MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Commit the edits! editor.commit(); }
}
- Using Shared Preferences
- shared preferences
- Shared Preferences
- Shared Preferences
- (一)Shared Preferences
- 数据存储【Shared Preferences
- Shared Preferences介绍
- Android Shared Preferences 安全性
- how to use shared Preferences
- 使用Shared Preferences存储数据
- 【Android数据存储】- Shared Preferences
- Shared preferences - Android 数据存储
- 使用Shared Preferences存储数据
- Shared Preferences(一)
- Shared Preferences(二)
- Shared Preferences(三)
- Android数据存储——Shared Preferences
- (一)Shared Preferences(2)
- linux2.6.20 sd/mmc卡驱动学习日记1(基于s3c2440)
- SQL Server Management Studio Express下载地址
- Windows XP中在“添加/删除windows组件”中丢失IIS
- 基于按annotation的hibernate主键生成策略
- java 事务
- Using Shared Preferences
- poj 1990【树状数组】
- 程序员:提高编程效率的技巧
- LaTeX 插入图片
- 乔布斯成功的七条秘诀
- 调试
- 《算法导论的Java实现》 7 堆排序
- windows下eclipse中,涉及源码编译的apk开发——使用系统签名机制
- ibrowse 可用性测试(包括时间测试函数)