Android
使用 Custom Tabs 将 Human Token Journey 集成到您的 Android 应用中。
Custom Tabs 在您应用内的浏览器标签页中启动 Human Token Journey。当旅程完成时,浏览器会重定向到您的自定义 URL scheme,您的应用会拦截它。
前提条件
- Android 8 (API 26) 或更高版本
androidx.browser:browser:1.5.0- 您应用的唯一 URL scheme(例如
myapp)
步骤 1: 添加依赖
在您的 build.gradle:
implementation("androidx.browser:browser:1.5.0")
步骤 2: 定义 URL scheme
选择一个唯一的 scheme(例如 myapp)。创建流程时将其用作 callbackUri:
callbackUri: myapp://callback
步骤 3: 创建 Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent
class HumanTokenActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val url = intent.getStringExtra(EXTRA_PROCESS_URL) ?: return finish()
CustomTabsIntent.Builder()
.build()
.launchUrl(this, Uri.parse(url))
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
intent.data?.let { uri ->
if (uri.scheme == "myapp") {
// Journey completed — send processId to your backend
val processId = uri.getQueryParameter("id")
handleResult(processId)
}
}
}
private fun handleResult(processId: String?) {
// Fetch the result from your backend via Get Process
finish()
}
companion object {
const val EXTRA_PROCESS_URL = "process_url"
}
}
步骤 4: 配置 AndroidManifest.xml
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<activity
android:name=".HumanTokenActivity"
android:launchMode="singleTop"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="callback" />
</intent-filter>
</activity>
singleTop 确保 Android 在现有 Activity 上调用 onNewIntent(),而不是创建新的。