tencent cloud

All product documents
Android Client SDK Integration
Last updated: 2025-03-25 11:16:49
Android Client SDK Integration
Last updated: 2025-03-25 11:16:49

Service Process Diagram



Preparations

Privacy Settings

Privacy Policy Link: Privacy Policy | Captcha.

Before adapting, integrating, or loading this SDK product into your product, application, or service, you should carefully read and agree to the relevant service agreements, these rules, and/or third-party developer compliance guidelines (or similar legal documents) that we have published, and conduct a compliance self-check on the collection and use of personal information by the product into which you adapt, integrate, or load this SDK product.

If you do not agree to any part of the Privacy Policy, you must immediately stop accessing and using the SDK Product and/or related services. You shall use this SDK Product and process end users' personal information only after obtaining their consent. Before obtaining end users' consent, you should not enable or initialize this SDK.

Captcha CaptchaAppid

When you access this SDK, it relies on the Captcha business ID - CaptchaAppid, which can be obtained through the Tencent Cloud Console verification management. The SDK will not function properly without it. If you do not have the corresponding CaptchaAppid, please create a new one in the Tencent Cloud Console. It will be used later in the required parameter settings for Captcha initialization, i.e., setCaptchaAppid (Captcha CaptchaAppid).

Android SDK Integration

1. Integration Methods
Add in AndroidManifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Add in ProGuard:
-keep class com.**.TNative$aa { public *; }
-keep class com.**.TNative$aa$bb { public *; }
-keep class com.**.TNative$bb { *; }
-keep class com.**.TNative$bb$I { *; }
2. AAR Integration
Copy the AAR file to the libs directory of the project and set the dependency. Add it in the build.gradle file.
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar'])
}
defaultConfig {
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
Choose armeabi-v7a or arm64-v8a based on actual needs. x86 and x86_64 architectures are not supported yet.

Captcha Integration

Declare to display the CAPTCHA tcaptchaWebview
<WebView
android:id="@+id/tcaptchaWebview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Captcha Initialization and Call Example
// Initialize
TencentCaptchaConfig.BuilderconfigBuilder =new
TencentCaptchaConfig.Builder(getApplicationContext(),
newITencentCaptchaPrivacyPolicy(){...},
newICaptchaDeviceInfoProvider(){...});
RetCoderet =TencentCaptcha.init(config.build());
...
// Required parameters
TencentCaptchaParam.BuildertencentCaptchaBuilder =new
TencentCaptchaParam.Builder
()
.setWebView(tcaptchaWebView)
.setCaptchaAppid(CaptchaAppid);
// Business start
TencentCaptcha.start(callback,tencentCaptchaBuilder.build());
Captcha Callback Response Example
TencentCaptchaCallback callback = new TencentCaptchaCallback() {
@Override
public void finish(RetCode ret, JSONObject resultObject) {...}

@Override
public void exception(Throwable t) {...}
};

SDK Class Description

Main Class Description
Class
Description
TencentCaptcha
Main entry class of SDK
TencentCaptchaConfig
SDK initialization configuration
TencentCaptchaCallback
SDK result callback
TencentCaptchaParam
SDK Captcha input parameters
TencentCaptcha Class (Main Entry) API Description
/***
Initialize with the built configuration
*
* @param TencentCaptchaConfig
* @return RetCode
*/
RetCode init(TencentCaptchaConfig);
/**
* Start Captcha detection
*
* @param TencentCaptchaCallback
* @param TencentCaptchaParam
* @return RetCode
*/
RetCode start(TencentCaptchaCallback, TencentCaptchaParam);
/**
* Interrupt detection process
*/
void stop();

TencentCaptchaConfig Class (Initialization Configuration) API Description

Create using Builder, supports chained calls.
/**
* Create a TencentCaptcha.Builder object.
*
* @param Context
* @param ITencentCaptchaPrivacyPolicy API instance, [must pass implementation], any API call of the SDK will use this instance to ask the user if they have agreed to the privacy policy
* @param ICaptchaDeviceInfoProvider API instance, pass ANDROIDID, used for the validity check of the device identifier itself and to identify abnormal device environments to detect device risks*/
*/
Builder(Context, ITencentCaptchaPrivacyPolicy,
ICaptchaDeviceInfoProvider);
TencentCaptchaCallback Class (Result Callback) API Description
/**
* Callback when Captcha verification ends, notifying the host to obtain verification ticket and other information.
*
* @param RetCode, when RetCode is not OK, please refer to [retcode values] section
* @param JSONObject, when RetCode is OK, please refer to [JSONObject return values] section for information
*/
void finish(RetCode, JSONObject);
/**
* Callback when an exception occurs in the SDK working thread
*
* @param Throwable
*/
void exception(Throwable);
TencentCaptchaParam Class (Input Parameters) API Description
Parameters class for starting Captcha.
/**
* Create a TencentCaptchaParam.Build object.
*
* @return TencentCaptchaParam
*/
TencentCaptchaParam build();
/**
* Set the CaptchaAppid, obtained from the tencent cloud console. Without this, the SDK will not work properly.
*
* @param String, CaptchaAppid.
* @return Build
*/
Builder setCaptchaAppid(String);
/**
* Set the WebView to display the Captcha.
*
* @param webView
* @return Builder
*/
Builder setWebView(WebView);

Sample Code

Creating the SDK Configuration Builder

TencentCaptchaConfig.Builder configBuilder = new
TencentCaptchaConfig.Builder(
getApplicationContext(),
new ITencentCaptchaPrivacyPolicy() {
@Override
public boolean userAgreement() {
// Authorization Passed
// true indicates authorization passed, the SDK can be used normally
// false indicates unauthorized, subsequent calls to the start method will throw an exception
return false;
}
},
new ICaptchaDeviceInfoProvider() {
@Override
public String getAndroidId() {
return androidId; // Implementation to get AndroidId
}
}
);

Creating the SDK Configuration Builder

// Initialize the SDK
RetCode ret = TencentCaptcha.init(configBuilder.build());
if(ret == RetCode.OK) {
// Initialized successfully
} else {
// Initialization failed
log.error("ret code: " + ret.getCode() + " msg:" + ret.getMsg()); }

Implementing Captcha Callback

TencentCaptchaCallback callback = new TencentCaptchaCallback() {
@Override
public void finish(RetCode ret, JSONObject resultObject) {
if (ret == RetCode.OK) {
// Success, execute business logic
} else {
// Failed
log.error("code:" + ret.getCode() + " msg:" + ret.getMsg());
}
}
@Override
public void exception(Throwable t) {
// Abnormal
t.printStackTrace();
}
};

Creating Captcha Input Parameters

TencentCaptchaParam.Builder paramBuilder = new
TencentCaptchaParam.Builder()
.setWebView(tcaptchaWebView) // required, WebView to display Captcha
.setCaptchaAppid("CaptchaAppid"); // required, CaptchaAppid

Starting Captcha

TencentCaptcha.start(callback, paramBuilder.build());

Interrupting Captcha (Typically Called When Exiting the Interface)

@Override
protected void onPause() {
super.onPause();
TencentCaptcha.stop();}

RetCode (Callback Return Value When Captcha Verification Ends)

Retcode Method

/**
* Get Error Code
*
* @return int
*/
int getCode();
/**
* Get Detailed Error Information
*
* @return String
*/
String getMsg();

Retcode Values

Retcode Values
code
Description
OK
0
Succeeded
INIT_FAILED
1
Initialization failed
NOT_INIT
2
Uninitialized
INVALID_PARAMS
3
Invalid Input Parameters
INVALID_WEBVIEW
4
Webview Is Empty
CAPTCHA_RETDATA_EMPTY
5
Captcha return parameter is empty
CAPTCHA_EXCEPTION
6
Captcha detection exception
POLICY_ERROR
7
Privacy terms not agreed

JSONObject (Callback Return Business Parameters When Captcha Verification Ends)

Field Name
Value Type
Description
ret
Int
Verification result, 0: Verification successful.

ticket
String
Ticket returned by Captcha, ticket has value only when ret = 0.

CaptchaAppId
String
Captcha Application ID.
bizState
Any
Custom pass-through parameters.
randstr
String
Random string for this verification, this parameter needs to be passed during subsequent ticket verification.
errorCode
Number
Error code, for details, see the callback function errorCode description.
errorMessage
String
Error message.


TencentCaptchaParam Class (Optional Parameter Configuration Description)

/**
* Custom pass-through parameters, this field can be used by the business to pass a small amount of data, and the content of this field will be included in the callback object.
*
* @param Object
* @return Builder
*/
Builder setBizState(Object);
/**
* true: Enable adaptive night mode.
*
* @param boolean
* @return Builder
*/
Builder setEnableDarkMode(boolean);
/**
* "force": Enable forced night mode.
*
* @param String
* @return Builder
*/
Builder setEnableDarkMode(String);
/**
* false: Hide the help button.
*
* @param boolean
* @return Builder
*/
Builder setNeedFeedBack(boolean);
/**
* Custom help link: "URL".
*
* @param String
* @return Builder
*/
Builder setNeedFeedBack(String);
/**
* Specify the language of the Captcha prompt text, which takes precedence over console configuration.
* Supports passing the same value as navigator.language, the user's preferred language, case-insensitive.
* For details, see https://www.tencentcloud.com/zh/document/product/1159/49680?lang=zh&pg=#userLanguage.
*
* @param String
* @return Builder
*/
Builder setUserLanguage(String);
/**
* true: enable aging adaptation
* false: disable aging adaptation
*
* @param boolean
* @return Builder
*/
Builder setEnableAged(boolean);
/**
* Define the Captcha display method.
* popup: (default) Popup, displays the Captcha in a centered floating layer.
* full: Full Screen
*
* @param String
* @return Builder
*/
Builder setType(String);
/**
* Whether to show loading box during Captcha loading. If this parameter is not specified, the loading box is shown by default.
* true: show loading box
* false: do not show loading box
*
* @param boolean
* @return Builder
*/
Builder setLoading(boolean);
/**
* Set CaptchaAppId encrypted verification string, see encryption verification capability access guide for details
*
* @param String
* @return Builder
*/
Builder setAidEncrypted(String);
/**
* Callback for some internal Captcha information, see [TencentCaptchaParam.OptionsCallback description] for details.
*
* @param OptionsCallback
* @return Builder
*/
Builder setOptionsCallback(OptionsCallback);

/**
* Support flexible settings for corresponding key and value.
*
* @param String, corresponding key value
* @param Object, corresponding value
* If you set the key to enableAutoCheck and the value to true to enable it, auto-completion of verify selection for safe requests can be automatically performed, no action required by the user. Disabled by default if not set.
* @return
*/
Builder setExtractParameter(String, Object);
configuration name
Value Type
Description
enableAutoCheck
true
Once enabled, auto-completion of verify selection for safe requests can be automatically performed, no action required by the user. Disabled by default.
Enable: {"enableAutoCheck":true}

TencentCaptchaParam.OptionsCallback Description

/**
* 1. When optFuncName is ready,
* data returns the callback when the Captcha is loaded, with the actual width and height of the Captcha (unit: px):
* {"sdkView": {
* "width": number,
* "height": number
* }}
* This parameter is only for viewing the Captcha dimensions, do not use this parameter to set the dimensions directly.
* 2. When optFuncName is showFn, data returns duration, rendering time + sid information.
*
* @param optFuncName, String
* @param data, String
*/
void onCallback(String optFuncName, String data); result callback, return value


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon