当前位置:网站首页>了解 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()方法保存这些更改。
边栏推荐
- Oracle检查点队列–实例崩溃恢复原理剖析
- Basic grammar of interview (Part 1)
- Poj 3237 Tree (Tree Chain Split)
- How to add new fields to mongodb with code (all)
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- Emotional analysis of wechat chat records on Valentine's day based on Text Mining
- "Chris Richardson microservices series" uses API gateway to build microservices
- Deeply convinced plan X - network protocol basic DNS
- SQL knowledge leak detection
- EBS Oracle 11g cloning steps (single node)
猜你喜欢

【愚公系列】2022年7月 Go教学课程 004-Go代码注释

每日刷题记录 (十四)

Emotional analysis of wechat chat records on Valentine's day based on Text Mining

Business learning of mall order module

QML reported an error expected token ";", expected a qualified name ID

2.2.5 basic sentences of R language drawing

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

Livelocks and deadlocks of concurrency control

ICMP 介绍
![[Yugong series] go teaching course in July 2022 004 go code Notes](/img/56/d596e7c7bec9abd888e8f18f9769f8.png)
[Yugong series] go teaching course in July 2022 004 go code Notes
随机推荐
regular expression
Sorting out the problems encountered in MySQL built by pycharm connecting virtual machines
Kingbasees v8r3 data security case - audit record clearing case
Image editor for their AutoLayout environment
Defect detection - Halcon surface scratch detection
EL与JSTL注意事项汇总
K210 learning notes (IV) k210 runs multiple models at the same time
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Advantages of robot framework
HYSBZ 2243 染色 (树链拆分)
SQL knowledge leak detection
Dbeaver executes multiple insert into error processing at the same time
854. 相似度为 K 的字符串 BFS
EBS Oracle 11g cloning steps (single node)
CRM creates its own custom report based on fetch
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
PIP install beatifulsoup4 installation failed
Database recovery strategy
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp