Logo Site EngageLab Mark Colored TransparentDocument
Search

Android SDK Integration Guide

Overview

The MTVerify Android SDK is a mobile number authentication SDK that provides fast, secure, carrier-grade phone number verification.

Key Features

  • SDK initialization: Automatically fetch configuration and initialize via AppKey
  • Coverage check: Check whether the current network environment supports the authentication service
  • Number authentication: Initiate mobile number authentication and obtain an authentication token

System Requirements

  • Android 5.0 (API 21) and above
  • The user's device has mobile data enabled

Integration Guide

Permission Configuration

The aar package requests the following permissions:

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
              
              <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

            
This code block in the floating window

Carrier Configuration

Because the carrier interface requires HTTP plaintext transmission, you need to configure the network security policy in your application's AndroidManifest.xml:

<application android:networkSecurityConfig="@xml/mtverify_network_security_config" ...> ... </application>
              
              <application
    android:networkSecurityConfig="@xml/mtverify_network_security_config"
    ...>
    ...
</application>

            
This code block in the floating window

You also need to copy the mtverify_network_security_config.xml file to your application's res/xml/ directory. You can find this file in the installation package you downloaded.

Method List

init

Initialize the MTVerify SDK and set the app key and environment configuration.

Method Signature

@JvmStatic fun init( context: Context, appkey: String, environment: IPEnvironment = IPEnvironment.PRODUCTION, callback: InitCallback? = null )
              
              @JvmStatic
fun init(
    context: Context, 
    appkey: String, 
    environment: IPEnvironment = IPEnvironment.PRODUCTION, 
    callback: InitCallback? = null
)

            
This code block in the floating window

Parameters

Parameter Type Required Description
context Context Yes Android context object
appkey String Yes App key, used to identify the app
environment IPEnvironment No Environment type, defaults to SANDBOX. Options: - IPEnvironment.SANDBOX: test/sandbox environment - IPEnvironment.PRODUCTION: production environment
callback InitCallback? No Initialization callback interface, used to receive the initialization result

Return Value

None

Callback Result

The initialization result is returned via InitCallback.onComplete(result: InitResult):

data class InitResult( val code: Int, // Status code; 0 means success, other numbers mean failure val message: String? // Message (success or error message) )
              
              data class InitResult(
    val code: Int,        // Status code; 0 means success, other numbers mean failure
    val message: String?  // Message (success or error message)
)

            
This code block in the floating window

Usage Example

MTVerifyApi.init( context = this, appkey = "your_app_key", environment = IPEnvironment.PRODUCTION, callback = object : InitCallback { override fun onComplete(result: InitResult) { if (result.code == 0) { // Initialization succeeded Log.d("MTVerify", "Init succeeded: ${result.message}") } else { // Initialization failed Log.e("MTVerify", "Init failed: ${result.message}") } } } )
              
              MTVerifyApi.init(
    context = this,
    appkey = "your_app_key",
    environment = IPEnvironment.PRODUCTION,
    callback = object : InitCallback {
        override fun onComplete(result: InitResult) {
            if (result.code == 0) {
                // Initialization succeeded
                Log.d("MTVerify", "Init succeeded: ${result.message}")
            } else {
                // Initialization failed
                Log.e("MTVerify", "Init failed: ${result.message}")
            }
        }
    }
)

            
This code block in the floating window

Notes

  • You must call the init() method to initialize the SDK before using other features
  • The initialization process fetches clientId and redirectUrl via an HTTP request and requires a network connection

setLogEnable

Set the log switch to control the SDK's log output.

Method Signature

@JvmStatic fun setLogEnable(enable: Boolean)
              
              @JvmStatic
fun setLogEnable(enable: Boolean)

            
This code block in the floating window

Parameters

Parameter Type Required Description
enable Boolean Yes true enables logging, false disables all log output

Usage Example

// Enable logging MTVerifyApi.setLogEnable(true) // Disable logging MTVerifyApi.setLogEnable(false)
              
              // Enable logging
MTVerifyApi.setLogEnable(true)

// Disable logging
MTVerifyApi.setLogEnable(false)

            
This code block in the floating window

checkCoverage

Check whether the specified mobile number supports the IPification verification service.

Method Signature

@JvmStatic fun checkCoverage( context: Context, phoneNumber: String, callback: CoverageCallback? = null )
              
              @JvmStatic
fun checkCoverage(
    context: Context,
    phoneNumber: String,
    callback: CoverageCallback? = null
)

            
This code block in the floating window

Parameters

Parameter Type Required Description
context Context Yes Android context object
phoneNumber String Yes Country code + user's mobile number
callback CoverageCallback? No Coverage check callback interface

Return Value

None

Callback Result

The check result is returned via CoverageCallback.onComplete(result: CoverageResult):

data class CoverageResult( val code: Int, // Status code; 0 means success, other numbers mean failure val isAvailable: Boolean, // Whether available (supported carrier) val operatorCode: String?, // Carrier code val message: String? // Message (success or error message) )
              
              data class CoverageResult(
    val code: Int,           // Status code; 0 means success, other numbers mean failure
    val isAvailable: Boolean, // Whether available (supported carrier)
    val operatorCode: String?, // Carrier code
    val message: String?      // Message (success or error message)
)

            
This code block in the floating window

Usage Example

MTVerifyApi.checkCoverage( context = this, phoneNumber = "6281234567890", callback = object : CoverageCallback { override fun onComplete(result: CoverageResult) { if (result.code == 0) { if (result.isAvailable) { // Service is supported Log.d("MTVerify", "Carrier supported: ${result.operatorCode}") } else { // Service is not supported Log.d("MTVerify", "Carrier not supported") } } else { // Check failed Log.e("MTVerify", "Check failed: ${result.message}") } } } )
              
              MTVerifyApi.checkCoverage(
    context = this,
    phoneNumber = "6281234567890",
    callback = object : CoverageCallback {
        override fun onComplete(result: CoverageResult) {
            if (result.code == 0) {
                if (result.isAvailable) {
                    // Service is supported
                    Log.d("MTVerify", "Carrier supported: ${result.operatorCode}")
                } else {
                    // Service is not supported
                    Log.d("MTVerify", "Carrier not supported")
                }
            } else {
                // Check failed
                Log.e("MTVerify", "Check failed: ${result.message}")
            }
        }
    }
)

            
This code block in the floating window

startAuthentication

Start the mobile number authentication flow.

Method Signature

@JvmStatic fun startAuthentication( activity: Activity, countryCode: String, phoneNumber: String, callback: AuthenticationCallback? = null )
              
              @JvmStatic
fun startAuthentication(
    activity: Activity,
    countryCode: String,
    phoneNumber: String,
    callback: AuthenticationCallback? = null
)

            
This code block in the floating window

Parameters

Parameter Type Required Description
activity Activity Yes Android Activity object, used to display the authentication UI
countryCode String Yes Country code
phoneNumber String Yes User's mobile number (without country code)
callback AuthenticationCallback? No Authentication callback interface

Return Value

None

Callback Result

The authentication result is returned via AuthenticationCallback.onComplete(result: AuthenticationResult):

data class AuthenticationResult( val code: Int, // Status code; 0 means success, -1 means failure, -2 means user canceled val token: String?, // Encrypted authentication data (a JSON string containing the code, AES-encrypted) val message: String? // Message (success or error message) )
              
              data class AuthenticationResult(
    val code: Int,        // Status code; 0 means success, -1 means failure, -2 means user canceled
    val token: String?,   // Encrypted authentication data (a JSON string containing the code, AES-encrypted)
    val message: String?  // Message (success or error message)
)

            
This code block in the floating window

Note: token is an encrypted JSON string in the following format:

{ "version": "1", "content": { "code": "auth code", "state": "state parameter", "appkey": "app key" } }
              
              {
  "version": "1",
  "content": {
    "code": "auth code",
    "state": "state parameter",
    "appkey": "app key"
  }
}

            
This code block in the floating window

Usage Example

MTVerifyApi.startAuthentication( activity = this, countryCode = "62", phoneNumber = "81234567890", callback = object : AuthenticationCallback { override fun onComplete(result: AuthenticationResult) { when (result.code) { 0 -> { // Authentication succeeded val token = result.token // The token is used to exchange for the result Log.d("MTVerify", "Auth succeeded: $token") } -1 -> { // Authentication failed Log.e("MTVerify", "Auth failed: ${result.message}") } -2 -> { // User canceled Log.d("MTVerify", "User canceled authentication") } } } } )
              
              MTVerifyApi.startAuthentication(
    activity = this,
    countryCode = "62",
    phoneNumber = "81234567890",
    callback = object : AuthenticationCallback {
        override fun onComplete(result: AuthenticationResult) {
            when (result.code) {
                0 -> {
                    // Authentication succeeded
                    val token = result.token
                    // The token is used to exchange for the result
                    Log.d("MTVerify", "Auth succeeded: $token")
                }
                -1 -> {
                    // Authentication failed
                    Log.e("MTVerify", "Auth failed: ${result.message}")
                }
                -2 -> {
                    // User canceled
                    Log.d("MTVerify", "User canceled authentication")
                }
            }
        }
    }
)

            
This code block in the floating window

Data Models

IPEnvironment

Environment type enum:

enum class IPEnvironment { SANDBOX, // Test/sandbox environment PRODUCTION // Production environment }
              
              enum class IPEnvironment {
    SANDBOX,      // Test/sandbox environment
    PRODUCTION    // Production environment
}

            
This code block in the floating window

InitResult

Initialization result:

data class InitResult( val code: Int, // Status code; 0 means success val message: String? // Message )
              
              data class InitResult(
    val code: Int,        // Status code; 0 means success
    val message: String?  // Message
)

            
This code block in the floating window

CoverageResult

Coverage check result:

data class CoverageResult( val code: Int, // Status code; 0 means success val isAvailable: Boolean, // Whether available val operatorCode: String?, // Carrier code val message: String? // Message )
              
              data class CoverageResult(
    val code: Int,           // Status code; 0 means success
    val isAvailable: Boolean, // Whether available
    val operatorCode: String?, // Carrier code
    val message: String?      // Message
)

            
This code block in the floating window

AuthenticationResult

Authentication result:

data class AuthenticationResult( val code: Int, // Status code; 0 means success, -1 means failure, -2 means user canceled val token: String?, // Encrypted authentication token val message: String? // Message )
              
              data class AuthenticationResult(
    val code: Int,        // Status code; 0 means success, -1 means failure, -2 means user canceled
    val token: String?,   // Encrypted authentication token
    val message: String?  // Message
)

            
This code block in the floating window

Callback Interfaces

InitCallback

Initialization callback interface:

interface InitCallback { fun onComplete(result: InitResult) }
              
              interface InitCallback {
    fun onComplete(result: InitResult)
}

            
This code block in the floating window

CoverageCallback

Coverage check callback interface:

interface CoverageCallback { fun onComplete(result: CoverageResult) }
              
              interface CoverageCallback {
    fun onComplete(result: CoverageResult)
}

            
This code block in the floating window

AuthenticationCallback

Authentication callback interface:

interface AuthenticationCallback { fun onComplete(result: AuthenticationResult) }
              
              interface AuthenticationCallback {
    fun onComplete(result: AuthenticationResult)
}

            
This code block in the floating window

Complete Example

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 1. Enable logging (optional) MTVerifyApi.setLogEnable(true) // 2. Initialize the SDK MTVerifyApi.init( context = this, appkey = "your_app_key", environment = IPEnvironment.SANDBOX, callback = object : InitCallback { override fun onComplete(result: InitResult) { if (result.code == 0) { // Initialization succeeded; you can start using other features checkCoverage() } } } ) } private fun checkCoverage() { // 3. Check coverage MTVerifyApi.checkCoverage( context = this, phoneNumber = "6281234567890", callback = object : CoverageCallback { override fun onComplete(result: CoverageResult) { if (result.isAvailable) { // 4. If supported, perform authentication startAuthentication() } } } ) } private fun startAuthentication() { // 5. Perform authentication MTVerifyApi.startAuthentication( activity = this, countryCode = "62", phoneNumber = "81234567890", callback = object : AuthenticationCallback { override fun onComplete(result: AuthenticationResult) { if (result.code == 0) { // Authentication succeeded; handle the token handleToken(result.token) } } } ) } private fun handleToken(token: String?) { // Call the API to handle the exchanged token } }
              
              class MainActivity : AppCompatActivity() {
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // 1. Enable logging (optional)
        MTVerifyApi.setLogEnable(true)
        
        // 2. Initialize the SDK
        MTVerifyApi.init(
            context = this,
            appkey = "your_app_key",
            environment = IPEnvironment.SANDBOX,
            callback = object : InitCallback {
                override fun onComplete(result: InitResult) {
                    if (result.code == 0) {
                        // Initialization succeeded; you can start using other features
                        checkCoverage()
                    }
                }
            }
        )
    }
    
    private fun checkCoverage() {
        // 3. Check coverage
        MTVerifyApi.checkCoverage(
            context = this,
            phoneNumber = "6281234567890",
            callback = object : CoverageCallback {
                override fun onComplete(result: CoverageResult) {
                    if (result.isAvailable) {
                        // 4. If supported, perform authentication
                        startAuthentication()
                    }
                }
            }
        )
    }
    
    private fun startAuthentication() {
        // 5. Perform authentication
        MTVerifyApi.startAuthentication(
            activity = this,
            countryCode = "62",
            phoneNumber = "81234567890",
            callback = object : AuthenticationCallback {
                override fun onComplete(result: AuthenticationResult) {
                    if (result.code == 0) {
                        // Authentication succeeded; handle the token
                        handleToken(result.token)
                    }
                }
            }
        )
    }
    
    private fun handleToken(token: String?) {
        // Call the API to handle the exchanged token
        
    }
}

            
This code block in the floating window

Notes

  1. Initialization order: You must call the init() method to initialize the SDK before using other features.
  2. Thread safety: All API methods can be called on the main thread, and callbacks are also executed on the main thread.
  3. Permission requirements: Make sure the app has requested the necessary network permissions.
  4. Token decryption: The token returned after successful authentication is encrypted and must be processed using the corresponding decryption method.
  5. Log control: It is recommended to disable log output in the production environment to improve performance and security.

Version Information

  • SDK version: 1.0.1
Icon Solid Transparent White Qiyu
Contact Sales