在生成启动链接和移动应用程序中,要确保设置了应用程序在AndroidManifest.xml中的关联属性。以下是一些可能有用的代码示例:
在AndroidManifest.xml中添加以下代码:
然后,在代码中检查data属性是否为null:
如果data为空,可以尝试从intent中获取数据。以下是代码示例:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intentData = intent.data // 获取intent的data属性
if (intentData != null) {
val appAuthConfig = getAppAuthConfiguration()
val authService = AuthorizationService(this)
val authIntent = authService.getAuthorizationRequestIntent(getAuthorizationRequest(appAuthConfig, intentData))
startActivityForResult(authIntent, RC_AUTH)
} else {
// 引导用户打开应用程序或手动输入URL
}
}
private fun getAuthorizationRequest(appAuthConfiguration: AppAuthConfiguration, intentData: Uri): AuthorizationRequest {
val codeVerifier = PKCEUtil.generateRandomCodeVerifier()
val extras = AuthorizationRequest.Builder(
appAuthConfiguration,
getString(R.string.client_id),
ResponseTypeValues.CODE,
intentData
)
.setScope(getString(R.string.scopes))
.setCodeVerifier(codeVerifier, codeVerifierChallengeMethod)
.build()
return extras
}