本页介绍了如何处理完整性判定方面的问题。
请求完整性令牌时,您可以选择向用户显示 Google Play 对话框。当存在一个或多个完整性判定方面的问题时,您可以显示该对话框。该对话框会显示在应用之上,并提示用户解决造成问题的原因。关闭该对话框后,您可以向 Integrity API 发出另一个请求以验证问题是否已解决。
请求完整性对话框
当客户端请求完整性令牌时,您可以使用 StandardIntegrityToken(标准 API)和 IntegrityTokenResponse(传统 API)中提供的方法:showDialog(Activity activity, int integrityDialogTypeCode)
。
以下步骤列出了使用 Play Integrity API 通过 GET_LICENSED 对话框代码显示补救措施对话框的方法。 本部分后面列出了您的应用可以请求的其他对话框代码。
从您的应用请求完整性令牌,并将该令牌发送到您的服务器。您可以使用标准请求或传统请求。
Kotlin
// Request an integrity token val tokenResponse: StandardIntegrityToken = requestIntegrityToken() // Send token to app server and get response on what to do next val yourServerResponse: YourServerResponse = sendToServer(tokenResponse.token())
Java
// Request an integrity token StandardIntegrityToken tokenResponse = requestIntegrityToken(); // Send token to app server and get response on what to do next YourServerResponse yourServerResponse = sendToServer(tokenResponse.token());
Unity
// Request an integrity token StandardIntegrityToken tokenResponse = RequestIntegrityToken(); // Send token to app server and get response on what to do next YourServerResponse yourServerResponse = sendToServer(tokenResponse.Token);
Unreal Engine
// Request an integrity token StandardIntegrityToken* Response = RequestIntegrityToken(); // Send token to app server and get response on what to do next YourServerResponse YourServerResponse = SendToServer(Response->Token);
原生
/// Request an integrity token StandardIntegrityToken* response = requestIntegrityToken(); /// Send token to app server and get response on what to do next YourServerResponse yourServerResponse = sendToServer(StandardIntegrityToken_getToken(response));
在您的服务器上,解密完整性令牌并检查
appLicensingVerdict
字段。结果可能如下所示:// Licensing issue { ... accountDetails: { appLicensingVerdict: "UNLICENSED" } }
如果令牌包含
appLicensingVerdict: "UNLICENSED"
,则回复您的应用客户端,请求其显示许可对话框:Kotlin
private fun getDialogTypeCode(integrityToken: String): Int{ // Get licensing verdict from decrypted and verified integritytoken val licensingVerdict: String = getLicensingVerdictFromDecryptedToken(integrityToken) return if (licensingVerdict == "UNLICENSED") { 1 // GET_LICENSED } else 0 }
Java
private int getDialogTypeCode(String integrityToken) { // Get licensing verdict from decrypted and verified integrityToken String licensingVerdict = getLicensingVerdictFromDecryptedToken(integrityToken); if (licensingVerdict.equals("UNLICENSED")) { return 1; // GET_LICENSED } return 0; }
Unity
private int GetDialogTypeCode(string IntegrityToken) { // Get licensing verdict from decrypted and verified integrityToken string licensingVerdict = GetLicensingVerdictFromDecryptedToken(IntegrityToken); if (licensingVerdict == "UNLICENSED") { return 1; // GET_LICENSED } return 0; }
Unreal Engine
private int GetDialogTypeCode(FString IntegrityToken) { // Get licensing verdict from decrypted and verified integrityToken FString LicensingVerdict = GetLicensingVerdictFromDecryptedToken(IntegrityToken); if (LicensingVerdict == "UNLICENSED") { return 1; // GET_LICENSED } return 0; }
原生
private int getDialogTypeCode(string integrity_token) { /// Get licensing verdict from decrypted and verified integrityToken string licensing_verdict = getLicensingVerdictFromDecryptedToken(integrity_token); if (licensing_verdict == "UNLICENSED") { return 1; // GET_LICENSED } return 0; }
在应用中,使用从服务器检索到的请求的代码调用
showDialog
:Kotlin
// Show dialog as indicated by the server val showDialogType: Int? = yourServerResponse.integrityDialogTypeCode() if (showDialogType != null) { // Call showDialog with type code, the dialog will be shown on top of the // provided activity and complete when the dialog is closed. val integrityDialogResponseCode: Task<Int> = tokenResponse.showDialog(activity, showDialogType) // Handle response code, call the Integrity API again to confirm that // verdicts have been resolved. }
Java
// Show dialog as indicated by the server @Nullable Integer showDialogType = yourServerResponse.integrityDialogTypeCode(); if (showDialogType != null) { // Call showDialog with type code, the dialog will be shown on top of the // provided activity and complete when the dialog is closed. Task<Integer> integrityDialogResponseCode = tokenResponse.showDialog(activity, showDialogType); // Handle response code, call the Integrity API again to confirm that // verdicts have been resolved. }
Unity
IEnumerator ShowDialogCoroutine() { int showDialogType = yourServerResponse.IntegrityDialogTypeCode(); // Call showDialog with type code, the dialog will be shown on top of the // provided activity and complete when the dialog is closed. var showDialogTask = tokenResponse.ShowDialog(showDialogType); // Wait for PlayAsyncOperation to complete. yield return showDialogTask; // Handle response code, call the Integrity API again to confirm that // verdicts have been resolved. }
Unreal Engine
// .h void MyClass::OnShowDialogCompleted( EStandardIntegrityErrorCode Error, EIntegrityDialogResponseCode Response) { // Handle response code, call the Integrity API again to confirm that // verdicts have been resolved. } // .cpp void MyClass::RequestIntegrityToken() { UStandardIntegrityToken* Response = ... int TypeCode = YourServerResponse.integrityDialogTypeCode(); // Create a delegate to bind the callback function. FShowDialogStandardOperationCompletedDelegate Delegate; // Bind the completion handler (OnShowDialogCompleted) to the delegate. Delegate.BindDynamic(this, &MyClass::OnShowDialogCompleted); // Call ShowDialog with TypeCode which completes when the dialog is closed. Response->ShowDialog(TypeCode, Delegate); }
原生
// Show dialog as indicated by the server int show_dialog_type = yourServerResponse.integrityDialogTypeCode(); if (show_dialog_type != 0) { /// Call showDialog with type code, the dialog will be shown on top of the /// provided activity and complete when the dialog is closed. StandardIntegrityErrorCode error_code = IntegrityTokenResponse_showDialog(response, activity, show_dialog_type); /// Proceed to polling iff error_code == STANDARD_INTEGRITY_NO_ERROR if (error_code != STANDARD_INTEGRITY_NO_ERROR) { /// Remember to call the *_destroy() functions. return; } /// Use polling to wait for the async operation to complete. /// Note, the polling shouldn't block the thread where the IntegrityManager /// is running. IntegrityDialogResponseCode* response_code; error_code = StandardIntegrityToken_getDialogResponseCode(response, response_code); if (error_code != STANDARD_INTEGRITY_NO_ERROR) { /// Remember to call the *_destroy() functions. return; } /// Handle response code, call the Integrity API again to confirm that /// verdicts have been resolved. }
对话框会显示在提供的 activity 之上。用户关闭对话框后,使用响应代码完成 Task。
(可选)请求另一个令牌以显示任何其他对话框。如果您发出的是标准请求,则需要再次预热令牌提供程序以获取新的判定结果。
完整性对话框代码
GET_LICENSED(类型代码 1)
判定问题
此对话框适用于以下两种问题:
- 未经授权的访问:
appLicensingVerdict: "UNLICENSED"
。这意味着用户账号没有您的应用的使用权,如果用户旁加载了您的应用,或者从 Google Play 以外的其他应用商店获取了您的应用,就会发生这种情况。 - 篡改的应用:
appRecognitionVerdict: "UNRECOGNIZED_VERSION"
。这意味着,您应用的二进制文件已被修改,或者不是 Google Play 认可的版本。
补救措施
您可以显示 GET_LICENSED
对话框,提示用户从 Google Play 获取正版应用。此单个对话框可同时处理这两种情况:
- 对于没有许可的用户,它会授予用户 Play 许可。这可让用户从 Google Play 接收应用更新。
- 对于使用篡改应用版本的用户,它会引导他们从 Google Play 安装未修改的应用。
当用户完成对话框后,后续的完整性检查会返回 appLicensingVerdict: "LICENSED"
和 appRecognitionVerdict: "PLAY_RECOGNIZED"
。
用户体验示例

CLOSE_UNKNOWN_ACCESS_RISK(类型代码 2)
判定问题
当 environmentDetails.appAccessRiskVerdict.appsDetected
包含 "UNKNOWN_CAPTURING"
或 "UNKNOWN_CONTROLLING"
时,表示设备上正在运行未知应用,这些应用可能会截屏或控制设备。
补救措施
您可以显示 CLOSE_UNKNOWN_ACCESS_RISK
对话框,提示用户关闭所有可能会截屏或控制设备的未知应用。
如果用户点按 Close all
按钮,所有此类应用都会关闭。
用户体验示例

CLOSE_ALL_ACCESS_RISK(类型代码 3)
判定问题
如果 environmentDetails.appAccessRiskVerdict.appsDetected
包含 "KNOWN_CAPTURING"
、"KNOWN_CONTROLLING"
、"UNKNOWN_CAPTURING"
或 "UNKNOWN_CONTROLLING"
中的任何一项,则表示设备上正在运行的应用可能会截屏或控制设备。
补救措施
您可以显示 CLOSE_ALL_ACCESS_RISK
对话框,提示用户关闭所有可能在截屏或控制设备的 app。如果用户点按 Close all
按钮,设备上所有此类应用都会关闭。
用户体验示例
