为什么我的应用抛出 IllegalStateException GoogleApiClient is not connected yet?

Why is my app throwing IllegalStateException GoogleApiClient is not connected yet?

java.lang.IllegalStateException: GoogleApiClient is not connected yet.

这来自我的服务 (GetInformationService.onConnected)。奇怪的是它在我的 onConnected 方法中说它没有连接?我已经看到很多这样的问题,但我仍然无法弄清楚,因为所有答案都说了一些关于 onCreate 方法的内容,但我没有 onCreate 因为这是一项服务。这是我的相关代码(请记住我正在扩展 IntentService):

@Override
protected void onHandleIntent(Intent intent)
{
    g = Globals.getInstance();
    context = this;


    locationProvider = LocationServices.FusedLocationApi;
    googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    locationRequest = new LocationRequest();
    locationRequest.setInterval(60000);
    locationRequest.setFastestInterval(15000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    googleApiClient.connect();
}


@Override
public void onConnected(Bundle bundle) 
{
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}

尝试在您的服务中包含 onCreate() 并从此处构建 googApiClient

    @Override
public void onCreate() {
    super.onCreate();
    googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(LocationServices.API)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .build();
    googleApiClient.connect();
      }