当前位置:网站首页>了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
2022-07-05 22:00:00 【知识大胖】
在 Android 中存储少量数据,如电话号码、姓名或人等。Android 开发人员使用SharedPreferencesAPI。但出于安全和异步交易的目的,谷歌建议使用 DataStore 。
SharedPreferences
这些SharedPreferencesAPIdata用于读取和写入存储在Android 设备目录中基于 XML 的文件中的键值对。SharedPreferences允许应用程序开发人员存储少量数据,如 String、Int、Long、Double 等,用于配置、设置等。您仍然可以通过 放置复杂的自定义类对象,Serializable但通常不建议这样做。
要使用它,首先您需要获取一个实例,SharedPreferences然后您可以使用get*()下面示例中的方法读取数据。
val preferences = this.getSharedPreferences("SOME_NAME", Context.MODE_PRIVATE)
// Reading the values from Preferences
val myStr = preferences.getString("myStrKey", "DEFAULT_STR")
val myLong = preferences.getLong("myLongKey", 0)
val myInt = preferences.getInt("myIntKey", 1)
val myDouble = preferences.getDouble("myDoubleKey", 0.0)
或者您可以通过访问SharedPreferences.Editor对象和调用put*()方法将值写入其中,最后您可以通过commitorapply()方法保存这些更改。
边栏推荐
- Business learning of mall order module
- PyGame practical project: write Snake games with 300 lines of code
- Detailed explanation of memset() function usage
- Index optimization of performance tuning methodology
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- HDU 4391 paint the wall segment tree (water
- 854. String BFS with similarity K
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Shell script, awk condition judgment and logic comparison &||
- QML reported an error expected token ";", expected a qualified name ID
猜你喜欢
Defect detection - Halcon surface scratch detection
Recovery technology with checkpoints
Overview of concurrency control
EBS Oracle 11g 克隆步骤(单节点)
Matlab | app designer · I used Matlab to make a real-time editor of latex formula
Serializability of concurrent scheduling
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
AD637使用笔记
SQL knowledge leak detection
Concurrency control of performance tuning methodology
随机推荐
CRM creates its own custom report based on fetch
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Experienced inductance manufacturers tell you what makes the inductance noisy. Inductance noise is a common inductance fault. If the used inductance makes noise, you don't have to worry. You just need
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
C language knowledge points link
阿龙的感悟
Advantages of robot framework
Codeforces 12D ball tree array simulation 3 sorting elements
854. String BFS with similarity K
Scenario interview: ten questions and ten answers about distributed locks
NET中小型企业项目开发框架系列(一个)
AD637 usage notes
Huawei cloud modelarts text classification - takeout comments
Defect detection - Halcon surface scratch detection
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
The solution to the problem that Oracle hugepages are not used, causing the server to be too laggy
Granularity of blocking of concurrency control
华为快游戏调用登录接口失败,返回错误码 -1
Meituan dynamic thread pool practice ideas, open source