title: Android geofence stop working when app is not running
body: <p>I'm working on an Android app that uses Google Geofence API.
  My code seems to work fine while the app is on foreground, but as soon as the app move to Pause state the geofence broadcast receiver stop receiving events.
  I'm pretty sure that the geofence is getting removed when the app move to background but I don't know why this is happening.</p>
  
  <p>Breafly, I create a list of geofences</p>
  
  <pre><code>Geofence geofence = new Geofence.Builder()
              .setRequestId(id)
              .setCircularRegion(lat, lng, GEOFENCE_RADIUS)
              .setExpirationDuration(Geofence.NEVER_EXPIRE)
              .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL)
              .setLoiteringDelay(1000)
              .build();
          geofences.add(geofence);
  </code></pre>
  
  <p>Then I create a request</p>
  
  <pre><code>geofenceRequest = new GeofencingRequest.Builder()
          .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
          .addGeofences(geofences)
          .build();
  </code></pre>
  
  <p>At this point I add the geofence</p>
  
  <pre><code>LocationServices.GeofencingApi.addGeofences(googleApiClient, request, createGeofencePendingIntent()).setResultCallback(this);
  </code></pre>
  
  <p>And finally I start the broadcast receiver</p>
  
  <pre><code>String GEOFENCE_ACTION = "gac.broadcast.geofence";
      if (geoFencePendingIntent != null)
      {
          GoogleApiClientManager.setGeofencePendingIntent(geoFencePendingIntent);
          return geoFencePendingIntent;
      }
      Intent intent = new Intent(GEOFENCE_ACTION);
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), GEOFENCE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      GoogleApiClientManager.setGeofencePendingIntent(pendingIntent);
      return pendingIntent;
  </code></pre>
  
  <p>As I've already said, everything work perfectly while running in foreground, but as soon as the app moves to background the geofence event stop being sent to receiver.</p>
  
  <p><strong>ADDITIONAL INFOS</strong> (not related to broadcast receiver issue):
  I tried to do the following:</p>
  
  <ul>
  <li>I start the map fragment and the geofence is working fine, so the app show a notification</li>
  <li>I move to home screen so the app move to Pause state and no notification is no longer received</li>
  <li>I move back to the app checking that location update and google api client are still working, and they actually work</li>
  <li>Finally geofencing is not working anymore</li>
  </ul>
  
  <p>Hope that everything is clear and that someone of you can help me.
  Thanks in advance</p>