Overview

GeeTest captcha Android SDK supports for native Android. The SDK doesn’t contain any third-party library.

Development environment

Item Description
Target Android 4.0+
Development environment Android Studio 2.1.3
System dependency android.support.v7.appcompat
Third-party dependency None
Dependency in demo None

Resources

Item Description
Data communication flow chart Data communication flow chart , Interaction process
Download SDK gt3-android-sdk
SDK API reference Check gt3-android-docs or the comments in examples
Error code Error code
Demo project download Demo project

Installation

Download SDK

Please click the link below to download the latest SDK (in .zip format).

gt3-android-sdk

Import SDK

Except using the remote repository, you can also drag and drop the .aar file into libs folder.

import

After adding the .aar file into the libs folder, please check if .aar has been successfully imported into Library. Please insert the following code into the build.gradle of your project.

repositories {
flatDir {
dirs 'libs'
}
}

Then, add dependency for .aar package manually.

compile(name: 'geetest_sensebot_android_vx.y.z_date', ext: 'aar')

libraries

Configure API

Android sdk provide the following APIs.

  1. captcha initialization
  2. Start captcha verification
  3. Get the verification callback data

Please firstly integrate server SDK, configure the id and key (get from GeeTest dashboard), then implement API1 and API2 in the captcha initialization method.

Please refer to the following code example for integration.

Compile and run your project

Compile your project and test GeeTest captcha.

build

Two integration options

  1. Bind GeeTest captcha to a custom button

    sample1

  2. Integrate with GeeTest captcha button

    sample2

Code example

File configuration

  1. Set permission

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  2. Proguard rules

    # GeeTest SDK has already been obfuscated, please do not obfuscate it again  
    -dontwarn com.geetest.sdk.**
    -keep class com.geetest.sdk.**{*;}

    # Tencent x5 SDK has been confused, please do not confuse again
    -dontwarn com.tencent.**
    -keep class com.tencent.**{*;}
  3. x5 Kernel initialization

    # Initialize the tencent x5 kernel in the Application's onCreate() method
    QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

    @Override
    public void onViewInitFinished(boolean arg0) {
    // TODO Auto-generated method stub
    //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。
    Log.e("appxxxx", " onViewInitFinished is " + arg0);
    }

    @Override
    public void onCoreInitFinished() {
    // TODO Auto-generated method stub
    }
    };
    //x5内核初始化接口
    QbSdk.initX5Environment(getApplicationContext(), cb);

Bind GeeTest captcha to a custom button

  1. New a captcha object

    Please new the object within the lifecycle of oncreate

    // 务必在oncreate方法里处理
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  1. Configure parameters for captcha initialization

    Configure it in the life cycle of oncreate or configure it when it has been called

    // 配置bean文件,也可在oncreate初始化
    gt3ConfigBean = new GT3ConfigBean();
    // 设置验证模式,1:bind,2:unbind
    gt3ConfigBean.setPattern(1);
    // 设置点击灰色区域是否消失,默认不消息
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // 设置debug模式
    gt3ConfigBean.setDebug(false);
    // 设置语言,如果为null则使用系统默认语言
    gt3ConfigBean.setLang(null);
    // 设置加载webview超时时间,单位毫秒,默认10000,仅且webview加载静态文件超时,不包括之前的http请求
    gt3ConfigBean.setTimeout(10000);
    // 设置webview请求超时(用户点选或滑动完成,前端请求后端接口),单位毫秒,默认10000
    gt3ConfigBean.setWebviewTimeout(10000);
    // 设置回调监听
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // 开启验证
    gt3GeetestUtils.startCustomFlow();

Integrate with GeeTest captcha button

  1. Insert captcha button

    <com.geetest.sdk.views.GT3GeetestButton
    android:id="@+id/btn_geetest"
    android:layout_width="290dp"
    android:layout_height="44dp"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="horizontal" />
  2. New a captcha object

    Please new the object in the life cycle of oncreate

    // 务必在oncreate方法里配置
    GT3GeetestButton geetestButton = (GT3GeetestButton) findViewById(R.id.btn_geetest);
    GT3GeetestUtils gt3GeetestUtils = new GT3GeetestUtils(this);
  3. Configure parameter for captcha initialization

    Configure it in the life cycle of oncreate or configure it when it has been called

    // 配置bean文件,也可在oncreate初始化
    gt3ConfigBean = new GT3ConfigBean();
    // 设置验证模式,1:bind,2:unbind
    gt3ConfigBean.setPattern(2);
    // 设置点击灰色区域是否消失,默认不消息
    gt3ConfigBean.setCanceledOnTouchOutside(false);
    // 设置debug模式
    gt3ConfigBean.setDebug(false);
    // 设置语言,参考API文档。如果为null则使用系统默认语言(TODO 若想设置语言立即生效,则init需要在setContentView之前)
    gt3ConfigBean.setLang(null);
    // 设置加载webview超时时间,单位毫秒,默认10000,仅且webview加载静态文件超时,不包括之前的http请求
    gt3ConfigBean.setTimeout(10000);
    // 设置webview请求超时(用户点选或滑动完成,前端请求后端接口),单位毫秒,默认10000
    gt3ConfigBean.setWebviewTimeout(10000);
    // 设置回调监听
    gt3ConfigBean.setListener(new GT3Listener());
    gt3GeetestUtils.init(gt3ConfigBean);
    // 绑定
    geetestButton.setGeetestUtils(gt3GeetestUtils);

Handle captcha callback

To deal with the callback data during the verification, you need to process API1 request via onButtonClick method, and process API2 request via onDialogResult method.

	
gt3ConfigBean.setListener(new GT3Listener() {

/**
* api1结果回调
* @param result
*/
@Override
public void onApi1Result(String result) {
Log.e(TAG, "GT3BaseListener-->onApi1Result-->" + result);
}

/**
* 验证码加载完成
* @param duration 加载时间和版本等信息,为json格式
*/
@Override
public void onDialogReady(String duration) {
Log.e(TAG, "GT3BaseListener-->onDialogReady-->" + duration);
}

/**
* 验证结果
* @param result
*/
@Override
public void onDialogResult(String result) {
Log.e(TAG, "GT3BaseListener-->onDialogResult-->" + result);
// 开启api2逻辑
new RequestAPI2().execute(result);
}

/**
* api2回调
* @param result
*/
@Override
public void onApi2Result(String result) {
Log.e(TAG, "GT3BaseListener-->onApi2Result-->" + result);
}

/**
* 统计信息,参考接入文档
* @param result
*/
@Override
public void onStatistics(String result) {
Log.e(TAG, "GT3BaseListener-->onStatistics-->" + result);
}

/**
* 验证码被关闭
* @param num 1 点击验证码的关闭按钮来关闭验证码, 2 点击屏幕关闭验证码, 3 点击返回键关闭验证码
*/
@Override
public void onClosed(int num) {
Log.e(TAG, "GT3BaseListener-->onClosed-->" + num);
}

/**
* 验证成功回调
* @param result
*/
@Override
public void onSuccess(String result) {
Log.e(TAG, "GT3BaseListener-->onSuccess-->" + result);
}

/**
* 验证失败回调
* @param errorBean 版本号,错误码,错误描述等信息
*/
@Override
public void onFailed(GT3ErrorBean errorBean) {
Log.e(TAG, "GT3BaseListener-->onFailed-->" + errorBean.toString());
}

@Override
public void onButtonClick() {
new RequestAPI1().execute();
}

});

Please refer to the demo project for detailed examples.