GoogleApiClient is throwing “GoogleApiClient is not connected yet” AFTER onConnected function getting called

前端 未结 9 1446
-上瘾入骨i
-上瘾入骨i 2020-11-27 04:38

So I found something that is not very clear for me about GoogleApiClient. GoogleApiClient has a function called onConnected which is run wh

相关标签:
9条回答
  • 2020-11-27 05:35

    I just noticed that you are creating the googleApiClient in onStartCommand(). This seems like a bad idea.

    Let's say that your service gets triggered twice. Two googleApiClient objects will get created, but you'll only have reference to one. If the one whose reference you don't have executes its callback to onConnected(), you will be connected in that client but the client whose reference you actually do have could still be unconnected.

    I suspect that's what's going on. Try moving your googleApiClient creation to onCreate and see if you get the same behavior.

    0 讨论(0)
  • 2020-11-27 05:36

    moving the googleApi creation to onCreate resolved the issue for me.

    0 讨论(0)
  • 2020-11-27 05:38

    https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

    You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.

    The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

    0 讨论(0)
提交回复
热议问题