在我的代码中,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);
}
提前致谢
答案 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;
}
}