1

我尝试使用预热功能实现自定义选项卡意图。所以它需要创建CustomTabsServiceConnection. 但是,当我绑定服务时,它总是返回false

请在下面检查我的实现:

// Custom chrome tabs
private lateinit var customTabsIntent: CustomTabsIntent
private lateinit var customTabsClient: CustomTabsClient
private var connection: CustomTabsServiceConnection? = null

override fun onStart() {
    super.onStart()
    val ok = CustomTabsClient.bindCustomTabsService(this, CUSTOM_TAB_PACKAGE_NAME, ServiceConnection())
    Timber.i("Is OK = $ok")
}

inner class ServiceConnection: CustomTabsServiceConnection() {
    override fun onCustomTabsServiceConnected(
        name: ComponentName,
        client: CustomTabsClient
    ) {
        customTabsClient = client
        customTabsClient.warmup(0)

        // Connection callback
        val session = customTabsClient.newSession(object : CustomTabsCallback() {
            override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
                super.onNavigationEvent(navigationEvent, extras)
                Timber.i("Event = $navigationEvent")
            }
        })

        if (session != null) {
            session.mayLaunchUrl(Uri.parse(loginUrl), null, null)

            // Init custom tab intent
            val customTabBuilder = CustomTabsIntent.Builder(session)
            customTabsIntent = customTabBuilder.build()

            customTabsIntent.launchUrl(this, Uri.parse(loginUrl))
        }
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        connection = null
    }
}

override fun onStop() {
    super.onStop()
    connection?.let {
        this.unbindService(it)
    }
}

对此有任何想法吗?

4

0 回答 0