Клиент Google Api уже работает с идентификатором 0 - PullRequest
1 голос
/ 08 мая 2019

Я хочу подключить GoogleApiClient в действии.Он работает нормально, когда пользователь нажимает кнопку в первый раз, и появляется это диалоговое окно, но когда пользователь нажимает кнопку «Назад» и нажимает кнопку, отвечающую за инициализацию GoogleApiClient, я получаю эту ошибку

java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0

Google Api Client Dialog

Вот моя GoogleApiClient реализация

 private fun initGoogleApiClient() {
    mGoogleApiClient = GoogleApiClient.Builder(this)
        .addApi(Fitness.RECORDING_API)
        .addApi(Fitness.HISTORY_API)
        .addScope(Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
        .addConnectionCallbacks(this)
        .enableAutoManage(this, 0, this)
        .build()
     }

override fun onResume() {
    super.onResume()
    mGoogleApiClient?.let {
        if (!it.isConnected) it.connect()
    }
}

override fun onPause() {
    super.onPause()
    mGoogleApiClient?.let {
        it.stopAutoManage(this)
        if (it.isConnected) it.disconnect()
    }
}

override fun onConnected(bundle: Bundle?) {
    Timber.d("onConnected")
}

override fun onConnectionSuspended(p0: Int) {
    Timber.d("onConnectionSuspended")
}

override fun onConnectionFailed(connectionResult: ConnectionResult) {
    Timber.d("onConnectionFailed")
}

Вызов initGoogleApiClient метод, когда пользователь нажимает кнопку

btnApiClient.setOnClickListener {
        initGoogleApiClient()
}

Я проверил этот ответ и добавьте stopAutoManage метод, но все равно получите ту же ошибку.Также проверил документацию и там написано

public abstract void stopAutoManage (FragmentActivity lifecycleActivity)
Disconnects the client and stops automatic lifecycle management. Use this before creating a new client (which might be necessary when switching accounts, changing the set of used APIs etc.).

This method must be called from the main thread.

Поэтому я меняю свой метод initGoogleApiClient и добавляю метод stopAutoManage перед init, но на этот раз ничего не происходит после первого нажатия

private fun initGoogleApiClient() {

mGoogleApiClient?.stopAutoManage(this)

mGoogleApiClient = GoogleApiClient.Builder(this)
    .addApi(Fitness.RECORDING_API)
    .addApi(Fitness.HISTORY_API)
    .addScope(Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
    .addConnectionCallbacks(this)
    .enableAutoManage(this, 0, this)
    .build()

}

Также у меня есть только эти две зависимости

implementation 'com.google.android.gms:play-services-fitness:16.0.1'
implementation 'com.google.android.gms:play-services-auth:16.0.1'

Любая помощь будет оценена

...