GoogleApiClient.isConnected() return always false

浪子不回头ぞ 提交于 2019-11-28 06:50:50

问题


I'm trying to implement leaderboard in my game. I wrote this code:

GoogleApiClient mGoogleApiClient;
mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .build();

mGoogleApiClient.connect();
//....

while(!mGoogleApiClient.isConnected())Log.d("","NO CONNECTED");
Games.Leaderboards.submitScore(mGoogleApiClient, "MY_LEADERBOARD_ID",newscore);

Why mGoogleApiClient.isConnected() returns always false?

EDIT1: Ok, now it works (I have changed Drive.API with Games.API and Drive.SCOPE_FILE with Games.SCOPE_GAME) but when I call submitScore() even if it's all ok in the Log, the leaderboard is still empty. How can I check if submitScore() really works?

EDIT2: I used

startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient, "MY_LEADERBOARD_ID"), REQ_LEADERBOARD);

and everything works!


回答1:


You need to add a couple callback listeners to the Builder (or GoogleApiClient). What's most likely happening is that the login is failing because you don't have permissions yet. You need to handle that failure so that the user can authorize your app.

More reading: http://www.androidpolice.com/2014/02/14/for-developers-google-play-services-4-2-includes-new-client-api-model-consolidates-connections-under-one-object/




回答2:


I found out for some reason, although .connect() is asynchronous and that it would be logical to wait for a while loop immediately afterwards, it needs to apparently for some whatever reason still need to execute some things on the main UI thread, hence why your isConnected() is always returning false because that while loop is essentially blocking .connect(). If I could take a guess why I'm going to assume calling .connect() executes a piece of code which places itself in the main UI thread queue to be executed later on and by having that while loop there, you will be forever blocking that piece of code from ever executing/connecting.



来源:https://stackoverflow.com/questions/21966198/googleapiclient-isconnected-return-always-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!