Android SDK API
對外支持工具類
Method
getClient(Context)
獲取 GTCaptcha4Client 工具類對象
Declaration
public static GTCaptcha4Client getClient(Context context)
Parameters
| Param | Type | Description |
|---|---|---|
| context | context | 上下文 |
Discussion
注意:非單例實現,通過 getClient() 獲取的 GTCaptcha4Client 對象需要保存,以方便在 onDestory 或者其他生命週期使用
init(String)
使用默認配置初始化
Declaration
public GTCaptcha4Client init(String captchaId)
Parameters
| Param | Type | Description |
|---|---|---|
| captchaId | String | 在後台申請的 captchaId |
Discussion
注意:調用 init() 方法後就執行了加載驗證碼操作,若在 onResume() 生命週期之前調用,就預加載了驗證碼,當執行 verifyWithCaptcha() 展示驗證碼時候,則驗證碼很快加載展示出來。下同。
init(String, GTCaptcha4Config)
使用自定義配置初始化
Declaration
public GTCaptcha4Client init(String captchaId, GTCaptcha4Config config)
Parameters
| Param | Type | Description |
|---|---|---|
| captchaId | String | 在後台申請的 captchaId |
| config | GTCaptcha4Config | 參數配置對象 |
addOnSuccessListener(GTCaptcha4Client.OnSuccessListener)
添加驗證結果監聽對象,當驗證碼加載成功,用戶解答驗證碼結果回調
Declaration
public GTCaptcha4Client addOnSuccessListener(GTCaptcha4Client.OnSuccessListener response)
Parameters
| Param | Type | Description |
|---|---|---|
| response | GTCaptcha4Client.OnSuccessListener | 驗證結果監聽對象 |
addOnFailureListener(GTCaptcha4Client.OnFailureListener)
添加加載驗證碼失敗監聽對象,加載驗證碼的過程中報錯
Declaration
public GTCaptcha4Client addOnFailureListener(GTCaptcha4Client.OnFailureListener listener)
Parameters
| Param | Type | Description |
|---|---|---|
| listener | GTCaptcha4Client.OnFailureListener | 加載驗證碼失敗監聽對象 |
addOnWebViewShowListener(GTCaptcha4Client.OnWebViewShowListener)
添加加載驗證前端成功的監聽對象,驗證前端加載成功後回調。預加載已完成的情況下,調用verifyWithCaptcha後立即回調。
Declaration
public GTCaptcha4Client addOnWebViewShowListener(GTCaptcha4Client.OnWebViewShowListener listener)
Parameters
| Param | Type | Description |
|---|---|---|
| listener | GTCaptcha4Client.OnWebViewShowListener | 驗證前端加載成功監聽對象 |
verifyWithCaptcha()
開啟驗證流程,加載驗證碼
Declaration
public GTCaptcha4Client verifyWithCaptcha()
Discussion
注意:調用 verifyWithCaptcha() 之前需要先調用 init()、addOnSuccessListener()、addOnFailureListener 否則可能報錯
cancel()
取消驗證,關閉驗證窗口
Declaration
public void cancel()
Discussion
注意:調用 cancel() 之前需要先調用 init()、addOnSuccessListener()、addOnFailureListener 否則可能報錯
setLogEnable(boolean enable)
設置開啟/關閉打印日誌監控
Declaration
public void setLogEnable(boolean enable)
Discussion
調用 setLogEnable 會控制日誌打印功能
destroy()
銷毀生命週期長的資源
Declaration
public void destroy()
Discussion
銷毀驗證資源,務必調用
Discussion
該方法在客戶使用的驗證碼界面的 onDestroy 生命週期中調用
configurationChanged(Configuration)
橫豎屏切換
Declaration
public void configurationChanged(Configuration newConfig)
Discussion
該方法在橫豎屏切換生命週期 onConfigurationChanged 方法中調用
getVersion()
獲取版本號
Declaration
public static String getVersion()
Return Value
返回版本號,類型為 String
isSupportWebView(Context)
檢測是否支持系統 WebView,若不支持,則建議走兜底邏輯
Declaration
public static Pair<Boolean, String> isSupportWebView(Context context)
Return Value
Pair 對象說明
| Param | Type | Description |
|---|---|---|
| first | Boolean | 是否支持 WebView,若不支持則可以走兜底邏輯 |
| second | String | 加載 WebView 對象報錯說明 |
GTCaptcha4Config#Builder
參數配置類
setTimeOut(int)
設置請求超時時間
Declaration
public Builder setTimeOut(int timeOut)
Parameters
| Param | Type | Description |
|---|---|---|
| timeOut | int | 單位 ms,默認 10000 |
setResourcePath(String)
設置 WebView 加載資源地址
Declaration
public Builder setResourcePath(String html)
Parameters
| Param | Type | Description |
|---|---|---|
| html | String | WebView 加載地址 |
Discussion
默認加載 assets 目錄的 gt4-index*.html 文件,無特殊需求無需設置此接口
setLanguage(String)
傳輸 JS 的設置語言,此接口只能控制加載 WebView 的語言,SDK 的多語言跟隨當前應用
Declaration
public Builder setLanguage(String language)
Parameters
| Param | Type | Description |
|---|---|---|
| language | String | 前端支持語言短碼,詳見前端文檔 |
Discussion
如果設置為 null 或者未設置,則使用應用默認語言短碼傳輸給前端
setCanceledOnTouchOutside(boolean)
設置點擊灰色區域是否消失,默認消失
Declaration
public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside)
Parameters
| Param | Type | Description |
|---|---|---|
| canceledOnTouchOutside | boolean | true 為消失 |
setDebug(boolean)
設置是否是 debug 模式,上線務必置為 false,或者不配置
Declaration
public Builder setDebug(boolean debug)
Parameters
| Param | Type | Description |
|---|---|---|
| debug | boolean | true 為 debug,默認為 false |
setBackgroundColor(int)
設置 WebView 背景色,控制的是 Android WebView 控件的背景色,和前端 JS 的背景色不同
Declaration
public Builder setBackgroundColor(int backgroundColor)
Parameters
| Param | Type | Description |
|---|---|---|
| backgroundColor | int | 顏色資源地址或者 Color.parseColor() 返回值 |
setDialogStyle(String)
設置驗證碼對話框的主題樣式
Declaration
public Builder setDialogStyle(String dialogStyle)
Parameters
| Param | Type | Description |
|---|---|---|
| dialogStyle | String | 對話框的主題樣式名稱 |
Declaration
以下為默認主題樣式
<style name="gt4_captcha_dialog_style" parent="android:Theme.Dialog">
<!--Dialog的windowFrame框為無-->
<item name="android:windowFrame">@null</item>
<!--是否浮現在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否半透明-->
<item name="android:windowIsTranslucent">true</item>
<!--除去title-->
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<!--顯示區域以外是否使用黑色半透明背景-->
<item name="android:backgroundDimEnabled">true</item>
<!--設置dialog的背景-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:padding">0dp</item>
</style>
setDialogShowListener(GTCaptcha4Client.OnDialogShowListener)
設置驗證窗口顯示的監聽回調
Declaration
public void setDialogShowListener(GTCaptcha4Client.OnDialogShowListener dialogShowListener)
Parameters
| Param | Type | Description |
|---|---|---|
| dialogShowListener | GTCaptcha4Client.OnDialogShowListener | 驗證窗口顯示的監聽對象 |
setParams(Map<String, Object>)
額外參數配置,當前參數完全傳輸給 JS
Declaration
public Builder setParams(Map<String, Object> params)
Parameters
| Param | Type | Description |
|---|---|---|
| params | JSONObject | Additional configuration parameter |
Discussion
支持的鍵值如下表:
| Key | ValueType | Description | Default Value |
|---|---|---|---|
| displayMode | int | 主題樣式, 0: 跟隨系統 1: 普通 2: 暗黑 | 0 |
| protocol | String | 協議頭,支持 "https:// or http:// |
https:// |
| bgColor | String | 驗證背景蒙版顏色,遵循 css 規則,注意最後兩位為透明度 | #00000000 |
| loading | String | 自定義 loading,WebView 加載 html 的loading 資源相對路徑,空字符串表示取消loading效果 | "/gt4-loading.gif" |
| rem | float | 設置驗證碼整體的縮放比例 | 1 |
| hideBar | JSONArray | 隱藏後續驗證界面的關閉按鈕、刷新按鈕 | N/A |
| nextWidth | String | 驗證碼彈窗的寬度,需要攜帶單位,如"300px" | N/A |
| riskType | String | 結合風控融合,指定驗證形式 | N/A |
| hideSuccess | boolean | 隱藏驗證成功彈窗 | FALSE |
Sample Code
Map<String, Object> params = new HashMap<>();
params.put("displayMode", 0);
params.put("protocol", "https://");
params.put("bgColor", "#CC0000FF");
params.put("loading", "./loading_test.gif");
params.put("rem", 0.6);
JSONArray hideBar = new JSONArray()
.put("close")
.put("refresh");
params.put("hideBar", hideBar);
params.put("nextWidth", "300px");
params.put("hideSuccess", true);
GTCaptcha4Config.Builder builder = new GTCaptcha4Config.Builder()
.setParams(params);
build()
構造 GTCaptcha4Config 對象,傳輸給 init() 接口使用
Declaration
public GTCaptcha4Config build()
GTCaptcha4Client#OnSuccessListener
驗證結果回調監聽類
onSuccess(Boolean, String)
驗證接口回調
Declaration
void onSuccess(Boolean status, String response)
Parameters
| Param | Type | Description |
|---|---|---|
| status | Boolean | 用戶操作驗證結果,true 為成功,false 為失敗,失敗會刷新驗證樣式 |
| response | String | 用戶操作驗證成功,需要拿 response 數據到服務端驗證 |
Discussion
驗證成功描述如:
{"lot_number":"b150e61198924eff9eaae121e9b67aa8","pass_token":"6fcfba4c35cb57b873c99c5b3b02089c58a26244c18f7706aaa1e87aed3aa8f4","gen_time":"1622020720","captcha_output":"Tu6rW0dRNO0NLxMKftwdH00G5whF5HHddthoXSEA1G4="}
驗證失敗描述如:
{"captchaId":"647f5ed2ed8acb4be36784e01556bb71","captchaType":"slide","challenge":"95ed53b4-95c6-4c6b-9aac-769b29ef74a0"}
GTCaptcha4Client#OnFailureListener
加載失敗回調監聽類
onFailure(String)
加載失敗回調
Declaration
void onFailure(String error)
Parameters
| Param | Type | Description |
|---|---|---|
| error | String | 驗證加載失敗描述,注意:包括用戶主動取消 |
Discussion
加載失敗描述如:
{"code":"-14460","msg":"驗證會話已取消","desc":{"description":"User cancelled 'Captcha'"}}
GTCaptcha4Client#OnWebViewShowListener
驗證前端加載成功回調監聽類
onWebViewShow()
驗證前端加載成功
Declaration
void onWebViewShow()
Discussion
在驗證前端加載成功後回調。預加載已完成的情況下,調用verifyWithCaptcha後立即回調。
GTCaptcha4Client#OnDialogShowListener
驗證窗口顯示的監聽回調
actionBeforeDialogShow(Dialog)
驗證窗口在顯示前回調
Declaration
void actionBeforeDialogShow(Dialog dialog)
Parameters
| Param | Type | Description |
|---|---|---|
| dialog | Dialog | 驗證窗口實例 |
actionAfterDialogShow()
驗證窗口在顯示後回調
Declaration
void actionAfterDialogShow(Dialog dialog)
Parameters
| Param | Type | Description |
|---|---|---|
| dialog | Dialog | 驗證窗口實例 |
Discussion
可在此處對系統樣式進行改變,例如隱藏導航欄、隱藏狀態欄等
onDialogFocusChanged()
驗證窗口在改變焦點時回調
Declaration
void onDialogFocusChanged(Dialog dialog, boolean hasFocus)
Parameters
| Param | Type | Description |
|---|---|---|
| dialog | Dialog | 驗證窗口實例 |
| hasFocus | boolean | 是否有焦點 |










