Android Geofence eventually stop getting transition intents

So after playing around with this a bit, it looks like the ReceiveTransitionsIntentService as defined in the sample code will stop getting the notifications when the app is not around. I think this is a big problem with the example code… Seems like that will trip folks like me up.

So I used a broadcast receiver instead, and so far it seems to be working from my tests.

Add this to the manifest:

<receiver android:name=""
        <intent-filter >
            <action android:name=""/>

Then in the GeofenceRequester class you need to change the createRequestPendingIntent method so that it goes to your BroadcastReceiver instead of the ReceiveTransitionsIntentService

private PendingIntent createRequestPendingIntent() {

        // If the PendingIntent already exists
        if (null != mGeofencePendingIntent) {

            // Return the existing intent
            return mGeofencePendingIntent;

        // If no PendingIntent exists
        } else {

            // Create an Intent pointing to the IntentService
            Intent intent = new Intent("");
//            Intent intent = new Intent(context, ReceiveTransitionsIntentService.class);
             * Return a PendingIntent to start the IntentService.
             * Always create a PendingIntent sent to Location Services
             * with FLAG_UPDATE_CURRENT, so that sending the PendingIntent
             * again updates the original. Otherwise, Location Services
             * can't match the PendingIntent to requests made with it.
            return PendingIntent.getBroadcast(

Then I added the GeofenceReceiver class that looks something like this:

public class GeofenceReceiver extends BroadcastReceiver {
    Context context;

    Intent broadcastIntent = new Intent();

    public void onReceive(Context context, Intent intent) {
        this.context = context;


        if (LocationClient.hasError(intent)) {
        } else {

    private void handleError(Intent intent){
        // Get the error code
        int errorCode = LocationClient.getErrorCode(intent);

        // Get the error message
        String errorMessage = LocationServiceErrorMessages.getErrorString(
                context, errorCode);

        // Log the error

        // Set the action and error message for the broadcast intent
                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);

        // Broadcast the error *locally* to other components in this app

    private void handleEnterExit(Intent intent) {
        // Get the type of transition (entry or exit)
        int transition = LocationClient.getGeofenceTransition(intent);

        // Test that a valid transition was reported
        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
                || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

            // Post a notification
            List<Geofence> geofences = LocationClient
            String[] geofenceIds = new String[geofences.size()];
            String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
            String transitionType = GeofenceUtils

            for (int index = 0; index < geofences.size(); index++) {
                Geofence geofence = geofences.get(index);
       something with the geofence entry or exit. I'm saving them to a local sqlite db

            // Create an Intent to broadcast to the app
                    .putExtra(GeofenceUtils.EXTRA_GEOFENCE_ID, geofenceIds)


            // Log the transition type and a message
            Log.d(GeofenceUtils.APPTAG, transitionType + ": " + ids);

            // In debug mode, log the result
            Log.d(GeofenceUtils.APPTAG, "transition");

            // An invalid transition was reported
        } else {
            // Always log as an error

     * Posts a notification in the notification bar when a transition is
     * detected. If the user clicks the notification, control goes to the main
     * Activity.
     * @param transitionType
     *            The type of transition that occurred.
    private void sendNotification(String transitionType, String locationName) {

        // Create an explicit content Intent that starts the main Activity
        Intent notificationIntent = new Intent(context, MainActivity.class);

        // Construct a task stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        // Adds the main Activity to the task stack as the parent

        // Push the content Intent onto the stack

        // Get a PendingIntent containing the entire back stack
        PendingIntent notificationPendingIntent = stackBuilder
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        // Get a notification builder that's compatible with platform versions
        // >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(

        // Set the notification contents
                .setContentTitle(transitionType + ": " + locationName)

        // Get an instance of the Notification manager
        NotificationManager mNotificationManager = (NotificationManager) context

        // Issue the notification

Hopefully that helps someone else.

Following can be the reasons why the App is not getting Pending Intents according to the official google documentation –
1.The device is rebooted.
2.The app is uninstalled and re-installed.
3.The app’s data is cleared.
4.Google Play services data is cleared.
5.The app has received a GEOFENCE_NOT_AVAILABLE alert.(When Android Location Provider gets switched off)

You have to re-register the geofence after these events.

In my case Location provider gets switched off and also device gets rebooted that’s why I was not getting the pending intents.

Leave a Comment