头图

When the beautiful packaging of the new device is unpacked, the film on the new screen is removed, and the new device is set up, the applications that the user used before have been downloaded to the device. This is the magic of the new mobile device. However, when users try to use their apps but have to re-authenticate, the seamless experience of changing phones comes to an abrupt end. Let the user remember the account credentials and complete the account recovery process, which will cause the user to abandon the old account and create a new account, or even directly abandon the current application.

In order to solve this problem, we released the Block Store API, which makes re-logging into the user’s application on a new device as easy as restoring information from the backup during the setup process, so that the user can change the phone as if there is no Continue to use their apps before changing phones. Read on to learn more about Block Store. This article will introduce the benefits it brings and how to use Block Store to provide users with a magical experience.

What is Block Store?

The Block Store API allows your application to store user credentials so that they can be retrieved from new devices in the future and used to re-authenticate users. When a user uses one device to boot another device, credential data is transferred between devices.

The working principle of Block Store

  1. When a user logs in to your application (or at any time after this), you can save the authentication token you generated for the user to the Block Store.
  2. When you use the Block Store to save the token, the token will be encrypted and stored in the local storage of the device.
  3. When the user uses the "device-to-device" recovery process, the data will be transferred to the new device.
  4. If the user chooses to restore their data at the same time during the "device-to-device" restoration, when the user opens your app on a new device, Block Store will retrieve the token for your app.

Why use Block Store?

Although this API is optional, accessing it can bring the following benefits to your application:

  • If users do not have to bother to remember authentication credentials, they will also be more willing to use unique passwords that are more difficult to be phished.
  • Eliminate the usage resistance caused by login that may eventually lead to the loss of your users.
  • Integrating the Block Store is very simple, and it will work no matter how you log in.
  • Google will verify the identity of the user.

How to add it in my application?

When a user logs in to your application, you can store the authentication token you generated for the user in the Block Store by calling storeBytes(). This action will store the user's credentials to the source device. Now, the token has been encrypted and saved to the local storage of the device.

<!-- 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)
    }

When the user completes the "device-to-device" recovery process on the new device, the Block Store will retrieve your token. Since the user has agreed to restore your app’s data during the restoration process, no additional permission is required for this operation. When a user opens your application, you can request your token from the Block Store retrieveBytes() The obtained token can be used to keep the user logged in on the new device. If the application calling this interface does not have a token, Block Store will still call onSuccessListener() , but the result will be a null byte. storeBytes() and retrieveBytes() successively on the same device, retrieveBytes() will return the bytes set by the 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)
    }

summary

That's all the information you need to start using Block Store! Click here to learn more about

Welcome to click here to submit feedback to us, or share your favorite content or problems found. Your feedback is very important to us, thank you for your support!


Android开发者
404 声望2k 粉丝

Android 最新开发技术更新,包括 Kotlin、Android Studio、Jetpack 和 Android 最新系统技术特性分享。更多内容,请关注 官方 Android 开发者文档。