title: geofencing intent service not working android
body: <p>I am trying to develop a <strong>geofencing</strong> feature in my android app(Native android with java). I am using geofencing option for the first time and testing on <strong>real device</strong> (Redmi note4) with mocking locations with an app. <strong>Mock location</strong> works perfect but my <strong>pending intent service</strong> never calls onHandleIntent() method. I followed the android developers site to develop the functionality.</p>
  
  <p>Manifest</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  ....
  &lt;service android:name=".util.GeofenceTransitionsIntentService"/&gt;
  </code></pre>
  
  <p>Geofence Pending intent service</p>
  
  <pre><code>public class GeofenceTransitionsIntentService extends IntentService {
  /**
   * Creates an IntentService.  Invoked by your subclass's constructor.
   *
   * @param name Used to name the worker thread, important only for debugging.
   */
  
  private static final String TAG = "GeofenceTransitions";
  
  public GeofenceTransitionsIntentService() {
      super("GeofenceTransitions");
  }
  
  
  
  protected void onHandleIntent(Intent intent) {
      if(!isIntentServiceRunning) {
          isIntentServiceRunning = true;
      }
      GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
      if (geofencingEvent.hasError()) {
          String errorMessage = "Error" + geofencingEvent.getErrorCode();
          Log.e(Constants.TAG, errorMessage);
          return;
      }
  
      // Get the transition type.
      int geofenceTransition = geofencingEvent.getGeofenceTransition();
  
      // Test that the reported transition was of interest.
      if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
              geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
  
          // Get the geofences that were triggered. A single event can trigger
          // multiple geofences.
          List&lt;Geofence&gt; triggeringGeofences = geofencingEvent.getTriggeringGeofences();
  
          String geofenceTransitionDetails = "", geoTransition = "";
          if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
              // Get the transition details as a String.
              geoTransition = "Enter";
              geofenceTransitionDetails = "Enter transition";
          }
          if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT){
              geoTransition = "Exit";
              geofenceTransitionDetails = "Exit transition";
          }
  
          // Send notification and log the transition details.
          showNotification(geoTransition, geofenceTransitionDetails);
          Log.i(TAG, geofenceTransitionDetails);
      } else {
          // Log the error.
          showNotification("ERROR", "ERROR");
          Log.e(TAG, "NO ENTER OR EXIT");
      }
  }
  
  
  public void showNotification(String text, String bigText) {
  
      // 1. Create a NotificationManager
      NotificationManager notificationManager =
              (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
  
      // 2. Create a PendingIntent for AllGeofencesActivity
      Intent intent = new Intent(this, MainActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  
      // 3. Create and send a notification
      Notification notification = new NotificationCompat.Builder(this)
              .setSmallIcon(R.mipmap.ic_launcher)
              .setContentTitle(text)
              .setContentText(text)
              .setContentIntent(pendingNotificationIntent)
              .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
              .setPriority(NotificationCompat.PRIORITY_HIGH)
              .setAutoCancel(true)
              .build();
      notificationManager.notify(0, notification);
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      isIntentServiceRunning = false;
      Log.d("INTENET SERVICE", "ON DESTROY");
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
      isIntentServiceRunning = true;
      Log.d("INTENET SERVICE", "ON CREATE");
  }
  
  @Override
  public void onStart(@Nullable Intent intent, int startId) {
      super.onStart(intent, startId);
      Log.d("INTENET SERVICE", "ON START");
      isIntentServiceRunning = true;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Main activity, from where i add geofence.</p>
  
  <pre><code>mGeofencingClient = LocationServices.getGeofencingClient(this);
  GPSTracker gpsTracker = new GPSTracker(getApplicationContext());
      Location mLocation = gpsTracker.getLocation();
      if (mLocation != null) {
  
          Log.d(Constants.TAG, "Geofence Lat : " + mLocation.getLatitude() + " Lng : " + mLocation.getLongitude());
          mGeofenceList.add(new Geofence.Builder()
                  // Set the request ID of the geofence. This is a string to identify this
                  // geofence.
                  .setRequestId("placeId")
  
                  .setCircularRegion(
                          mLocation.getLatitude(),
                          mLocation.getLongitude(),
                          Constants.GEOFENCE_RADIUS_IN_METERS
                  )
                  .setExpirationDuration(Constants.GEOFENCE_EXPIRATION_IN_MILLISECONDS)
                  .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                          Geofence.GEOFENCE_TRANSITION_EXIT)
                  .build());
  
  
          mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
                  .addOnSuccessListener(this, new OnSuccessListener&lt;Void&gt;() {
                      @Override
                      public void onSuccess(Void aVoid) {
                          Log.d("GEOFENCE", "ADDED");
                          // Geofences added
                          // ...
                      }
                  })
                  .addOnFailureListener(this, new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          Log.d("GEOFENCE", "NOT ADDED");
                          // Failed to add geofences
                          // ...
                      }
                  });
      }
  
  
  
  private GeofencingRequest getGeofencingRequest() {
      GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
      builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
      builder.addGeofences(mGeofenceList);
      return builder.build();
  }
  
  private PendingIntent getGeofencePendingIntent() {
      // Reuse the PendingIntent if we already have it.
      if (mGeofencePendingIntent != null) {
          return mGeofencePendingIntent;
      }
      Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
      // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
      // calling addGeofences() and removeGeofences().
      mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
              FLAG_UPDATE_CURRENT);
      Log.d("GEOFENCE", "PENDING INTENT CREATED");
      return mGeofencePendingIntent;
  }
  </code></pre>
  
  <p><strong>GPSTracker</strong> is my custom utility class i am using to get location.</p>