什么是 Block Store?
Block Store 的工作原理
为什么要使用 Block Store?
虽然此 API 为可选接入,但是接入它可以为您的应用带来如下好处:
如何在我的应用中添加它?
<!-- Copyright 2021 Google LLC.SPDX-License-Identifier: Apache-2.0 -->val client = Blockstore.getClient(this)val token = StoreBytesData.Builder().setBytes(/* BYTE_ARRAY */).build()// storeBytes 应当在用户验证完成之后再调用client.storeBytes(token)).addOnSuccessListener{ result ->Log.d(TAG, “Stored: ${result} bytes”)}
当用户在新设备上完成 "设备到设备" 的恢复流程时,Block Store 会取回您的令牌。由于用户已经同意在恢复流程中恢复您应用的数据,所以此操作无需额外的许可。当用户打开您的应用时,您可以通过调用 retrieveBytes() 从 Block Store 请求您的令牌。取得的令牌可以用于在新设备上保持用户的登录状态。如果调用此接口的应用没有令牌,Block Store 依然会调用 onSuccessListener(),但结果会是空字节。如果您在同一个设备上先后调用 storeBytes() 和 retrieveBytes(),retrieveBytes() 会返回先前调用的 storeBytes() 设置的字节。
<!-- Copyright 2021 Google LLC.SPDX-License-Identifier: Apache-2.0 -->val client = Blockstore.getClient(this)client.retrieveBytes().addOnSuccessListener { result ->Log.d(TAG, “Retrieved: ${String(result)}”)}.addOnFailureListener { e ->Log.e(TAG, “Failed to retrieve bytes”, e)}
总结
欢迎您通过下方二维码向我们提交反馈,或分享您喜欢的内容、发现的问题。您的反馈对我们非常重要,感谢您的支持!
推荐阅读