Android¶
The SDK for Android embeds the widget in your app and reports the condition of the challenge with a listener.
Requirements¶
The following requirements must be met:
| Requirement | Value |
|---|---|
| Android | From version 4.1 Jelly Bean, API level 16 |
| Language | Kotlin or Java. The SDK is written in Kotlin. |
| Credentials | Public sitekey from the Details view |
Embed the SDK¶
Add these entries to the build.gradle file:
repositories {
mavenCentral()
}
dependencies {
implementation "eu.eucaptcha.android:eu-captcha-android:1.0.0"
// Or for `build.gradle.kts`
// implementation("eu.eucaptcha.android:eu-captcha-android:1.0.0")
}
Make the widget¶
Make the SDK and the widget in your activity:
import eu.eucaptcha.android.sdk.*
const val EU_CAPTCHA_SITEKEY = "EUCAPTCHA_SITE_KEY"
class MainActivity : ComponentActivity() {
private val sdk by lazy {
EuCaptchaSDK(context = this)
}
private val widget by lazy {
sdk.createWidget(sitekey = EU_CAPTCHA_SITEKEY)
}
}
Show the widget¶
Show the widget in your interface and read the condition:
val captchaResponse = remember { mutableStateOf("") }
var buttonEnabled by remember { mutableStateOf(false) }
widget.setOnStateChangeListener { event ->
captchaResponse.value = event.response
when (event.state) {
"completed" -> buttonEnabled = true
"expired" -> buttonEnabled = false
"error" -> buttonEnabled = true
}
}
AndroidView(factory = { _ -> widget.view })
Button(onClick = { widget.reset() }, enabled = buttonEnabled) {
Text("Submit")
}
The listener reports these conditions:
| Condition | Meaning |
|---|---|
completed |
The challenge passed. The response field contains the token. |
expired |
The token expired. |
error |
An error occurred during the verification. |
The widget.reset() call starts a new challenge.
Verify the token¶
Send the token to your server and verify it there. See Verify a client token.