大家好,我们这一节讲的是Android Preferences 的学习,Preferences 在Android当中被用来记录应用,以及用户喜好等等,它可以用来保存
简单的数据类型,如Int,Double,Boolean等。Preferences中保存的数据可以理解为Map型。我们通过PreferenceManager
以及getDefaultSharedPreferences(Context)
来获取它,比如当我们想获得整数我们可以用
getInt(String key, int defVal)
.获取里面的某个键值,当我们想修改时候我们用
putInt(String key, int newVal),
最后用
edit(),
方法提交!千万不要忘记了哦~
为了让大家跟好的理解我做了一个简单的Demo,程序主要有个TextView控件,上面写着用户使用改应用的次数。效果如下图所示:
下面是实现Demo的大体步骤:
一、新建一个Android工程命名为:PreferencesDemo。
二、在修改main.xml布局文件,这里只是在TextView控件里加了一个id.代码如下:
三、修改PreferenceDemo.java的代码,全部代码如下:
四、运行代码,实现上述效果.
五、查看Preferences文件,首先打开命令终端:adb shell一下,然后cd data/data进入该目录,ls一下我们会发现一大堆包文件,入下图所示:
cd com.android.tutor
(这里是我程序的包名)
/shared_prefs,ls一下会发现.xml文件如下图:
打开.xml文件,格式如下(为什么这样大家自己去理解):
OK,今天就到此为止,以上全是个人愚见,如果有什么地方不对的,请指正,谢谢大家!
分享到:
相关推荐
Android高手进阶教程之----Android 中Preferences的使用! .doc Android高手进阶教程之----Android 中自定义View的应用.doc Android高手进阶教程之----Android 中自定义属性(attr.xml,TypedArray)的使用! .doc ...
android-secure-preferences About This project uses the Encryption class from: http://www.java2s.com/Code/Android/Security/AESEncryption.htm Gives an implementation of SharedPreferences, which encrypts...
关于android的数据存储-SQLite-ContentProvider-preferences
Android Shared Preferences Helper Android Library to handle SharedPreferences boilerplate code and other tools Download dependencies { compile 'com.github.seanzor:shared-preferences-helper:1.1.0' }...
preferences-helper SharePreferences is very popular with any project and all most all project has SharePreferences for saving data. This library will help you faster in configuration and use ...
Reactive SharedPreferences for Android. Usage Create an RxSharedPreferences instance which wraps a SharedPreferences: SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences...
Multiplatform Preferences Use a single object : Preferences in your kotlin shared projects Compatible with kotlin android and kotlin native for iphone class MyPresenter { val preferences = ...
Android 使用Shared Preferences进行数据存储-样例,演示如何使用 Shared Preferences 获得数据和保存数据、如何使用getPreferences方法创建文件的模式,以及如何使用getPreferences模拟用户参数设置、查看 ...
的 Android SDK 提供了在 Android 平台上使用 JaVa 语言进行 Android 应用开发必须的工具和 API 接口。 特性 • 应用程序框架 支持组件的重用与替换 • Dalvik Dalvik Dalvik Dalvik 虚拟机 专为移动设备优化 • ...
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
This is Android Shared preference wrapper that encrypts the values of Shared Preferences using AES 128, CBC, and PKCS5 padding with integrity checking in the form of a SHA 256 hash. Each key is stored...
AndroidMaterialPreferences.zip,提供各种“材料设计”样式的首选项。提供各种“材料设计”样式的首选项
Android-secure-preferences.zip,android共享首选项包装器加密共享首选项的值。这不是防弹安全,而是一个快速的胜利,逐步使你的安卓应用程序更安全。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C...
FastSave-Android FastSave is An Android library for fast and easy access to Android Shared preferences. It allows you to save any type or list in the sharedpreferences and retrieve it in convenient ...
Android Debug Database is a powerful library for debugging databases and shared preferences in Android applications. Android Debug Database allows you to view databases and shared preferences directly...
Android-Shared-Preferences-example-application Android 共享首选项示例应用程序
下载NDK组件com.android.ide.eclipse.ndk_23.0.2.1259578.jar。将文件“com.android.ide.eclipse.ndk_23.0.2.1259578.jar”复制到sdk自带的eclipse/plugins目录下