更新时间:2022-02-03 11:15:21
在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME/shared_prefs 目录下。
数据读取
String PREFS_NAME = "Note.sample.roiding.com"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); String hello = settings.getString("hello", "Hi");
这段代码中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.roiding.sample.note" android:versionCode="1" android:versionName="1.0.0">
这里面的package。根据我目前的实验结果看,是这样的,欢迎指正。后面的那个int是用来声明读写模式,先不管那么多了,暂时就知道设为0(android.content.Context.MODE_PRIVATE)就可以了。
数据写入
String PREFS_NAME = "Note.sample.roiding.com"; SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", true); editor.putString("hello", "Hello~"); editor.commit();
有了上面数据读取的代码,这里面的就容易理解了,只是别忘了最后的commit();
原文(含示例代码):http://www.cnblogs.com/linzheng/archive/2011/01/22/1942073.html
链接: Android系列:http://www.cnblogs.com/linzheng/tag/android/