谷歌api客户端连接返回false

时间:2015-02-05 15:53:59

标签: android google-api-client

在我的代码中,google api客户端即使正在连接也会返回false值

GoogleApiClient mGoogleApiClient;
btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i(TAG,"btn is clicked");
            buildGoogleApiClient();
            Log.i(TAG,"build google api completed");
            mGoogleApiClient.connect();
            if(mGoogleApiClient.isConnected())
            Log.i(TAG,"client conneted");
            Log.i(TAG,String.valueOf(mGoogleApiClient.isConnected()));


        }
});

protected synchronized void buildGoogleApiClient() {
    Log.i(TAG, "Building GoogleApiClient");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    createLocationRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

提前致谢

2 个答案:

答案 0 :(得分:8)

mGoogleApiClient.connect();不是同步操作 - 这就是您在构建addConnectionCallbacks(this)时设置GoogleApiClient的原因。

mGoogleApiClient.isConnected()返回true之前,您必须等待onConnected()回调,然后才能使用GoogleApiClient

答案 1 :(得分:0)

使用此方法为您返回true。

public RequestAssync connect(){
    SystemStatus ss = Android.request.connection();
    if(ss.isConnect()){
        return ss.getStatus();
    }else{
        return false;
    }
}