使用 SMS Retriever API 自动进行短信验证
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 SMS Retriever API,您可以在 Android 应用中自动执行基于短信的用户验证,而无需用户手动输入验证码,也无需任何额外的应用权限。在应用中实现自动短信验证时,验证流程将如下所示:

- 用户在您的应用中启动短信验证。如果用户创建用户的帐号并不需要此信息,您的应用可能会提示用户提供手机号码或使用 Smart Lock(密码专用)提示选择器。
- 应用向服务器请求验证用户的电话号码。
根据用户数据库中提供的信息,此请求可能包含用户 ID 和/或用户电话号码。
- 同时,您的应用调用 SMS Retriever API 开始监听来自服务器的短信响应。
- 您的服务器向用户发送一条短信,其中包含要发回到服务器的一次性代码,以及用于标识应用的哈希值。
- 当用户的设备收到短信时,Google Play 服务使用应用哈希确定该短信是否适用于您的应用,并通过 SMS Retriever API 向您的应用提供短信文字。
- 应用从消息文本中解析出一次性代码并将其发送回服务器。
- 您的服务器从您的应用接收一次性代码,验证代码,并最终记录用户已成功验证其帐号。
如需在您的应用中实现自动短信验证,请参阅 Android 和服务器指南:
Android 指南
服务器指南
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThe SMS Retriever API enables automatic SMS verification in Android apps, eliminating manual code entry for users.\u003c/p\u003e\n"],["\u003cp\u003eThis API streamlines user verification by having your app listen for an SMS message containing a one-time code, sent by your server after a verification request.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Play services identifies the incoming SMS message as intended for your app using an app hash included in the message, ensuring secure delivery of the verification code.\u003c/p\u003e\n"],["\u003cp\u003eThe SMS Retriever API enhances user experience and security by automating verification without requiring extra app permissions.\u003c/p\u003e\n"]]],[],null,["# Automatic SMS Verification with the SMS Retriever API\n\nWith the SMS Retriever API, you can perform SMS-based user verification in your\nAndroid app automatically, without requiring the user to manually type\nverification codes, and without requiring any extra app permissions. When you\nimplement automatic SMS verification in your app, the verification flow looks\nlike this:\n\n[](/static/identity/sms-retriever/flow-overview.png)\n\n1. A user initiates SMS verification in your app. Your app might prompt the user to provide a phone number or use the [Smart Lock for Passwords hint selector](/identity/smartlock-passwords/android/retrieve-hints) if that information wasn't required to create the user's account.\n2. Your app makes a request to your server to verify the user's phone number. Depending on what information is available in your user database, this request might include the user's ID, the user's phone number, or both.\n3. At the same time, your app calls the SMS Retriever API to begin listening for an SMS response from your server.\n4. Your server sends an SMS message to the user that includes a one-time code to be sent back to your server, and a hash that identifies your app.\n5. When the user's device receives the SMS message, Google Play services uses the app hash to determine that the message is intended for your app, and makes the message text available to your app through the SMS Retriever API.\n6. Your app parses out the one-time code from the message text and sends it back to your server.\n7. Your server receives the one-time code from your app, verifies the code, and finally records that the user has successfully verified their account.\n\nTo implement automatic SMS verification in your app, see the Android and server\nguides:\n\n[Android Guide](/identity/sms-retriever/request)\n[Server Guide](/identity/sms-retriever/verify)"]]