title: Android - GoogleApiClient instance lost between .connect() and onConnected() in IntentService
body: <p>I'm trying to manage starting and stopping location updates (and other connected operations) from an IntentService triggered by changes to the enabled state of the GPS. The problem comes because the .connect() method on the GoogleApiClient runs asynchronously, which allows the service to die. By the time the callback is received for onConnected(), the static global variable mGoogleApiClient has been wiped. For removeLocationUpdates() I get:</p>
  
  <pre><code>java.lang.NullPointerException: GoogleApiClient must not be null
  </code></pre>
  
  <p>and for requestLocationUpdates() I get:</p>
  
  <pre><code>java.lang.IllegalStateException: GoogleApiClient is not connected yet.
  </code></pre>
  
  <p>What is the right way to retain the instance of the GoogleApiClient (which is a required parameter for both request and remove methods) between the death and rebirth of the IntentService? Or if that's the wrong question, what is the right way to accomplish what I'm trying to do?</p>
  
  <p>EDIT (19APR15) :
  Here are the relevant sections of code...</p>
  
  <p>From my class that extends WakefulBroadcastReceiver that listens for GPS events:</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
      // If user enables or disables GPS
      if(intent.getAction().equals(LocationManager.MODE_CHANGED_ACTION) || intent.getAction().equals(LocationManager.PROVIDERS_CHANGED_ACTION)) {
          Intent mIntent = new Intent(context, ActivationIntentService.class);
          mIntent.putExtra("enabled", isGPSEnabled(context));
          startWakefulService(context, mIntent);
      }
  }
  </code></pre>
  
  <p>From ActivationIntentService class that receives the intent passed from startWakefulService:</p>
  
  <pre><code>@Override
  protected void onHandleIntent(Intent intent) {
      if (intent != null) {
          this.intent = intent;
          if(intent.hasExtra("enabled")) {
              if(intent.getBooleanExtra("enabled", false)) {
                  buildGoogleApiClient();
              }
              else {
                  stopUpdates();
              }
          }
      }
  }
  
  protected synchronized void buildGoogleApiClient() {
      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(LocationServices.API)
              .addApi(ActivityRecognition.API)
              .build();
      mGoogleApiClient.connect();
  }
  
  protected LocationRequest getLocationRequest() {
      LocationRequest mLocationRequest = new LocationRequest();
      mLocationRequest.setInterval(5000);
      mLocationRequest.setFastestInterval(1000);
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
      return mLocationRequest;
  }
  
  @Override
  public void onConnected(Bundle bundle) {
      startLocationUpdates();
      startActivityRecognitionUpdates();
      ActivationReceiver.completeWakefulIntent(intent);
  }
  
  public void stopUpdates() {
      PendingIntent locationPendingIntent = PendingIntent.getService(this, 1, new Intent(this, LocationIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,locationPendingIntent);
      PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 2, new Intent(this, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
      ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mGoogleApiClient,activityRecognitionPendingIntent);
  }
  
  public void startLocationUpdates() {
      PendingIntent locationPendingIntent = PendingIntent.getService(this, 1, new Intent(this, LocationIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, getLocationRequest(), locationPendingIntent);
  }
  
  public void startActivityRecognitionUpdates() {
      PendingIntent activityRecognitionPendingIntent = PendingIntent.getService(this, 2, new Intent(this, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
      ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 5000, activityRecognitionPendingIntent);
  }
  </code></pre>