4

I'm trying to implement a Geofencing mechanism where a geofence is monitored and once the user exits the current geofence, the current co-ordinates are used to create a new geofence and db query is initiated for fetching some data.

My problem is that the pending intent is never fired.

From the logs i can see that the geofences are being added into the location client. However no pending intents are fired upon location change.(i've set the fence radius at 2m and i've walked over 100mts). Is there something wrong in the way i've declared the intent service ?

Here is the intent service class.

public class GeoFenceIntentService extends IntentService{
    private static final String mIntentName = "GeoFenceIntentService";

    public GeoFenceIntentService() {
        super(mIntentName);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        int transitionType = LocationClient.getGeofenceTransition(intent);
        Log.e(TAG,"Inside fence handler");
        if(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){
            //Query DB here with current co-ords
            //create new GeoFence
            Location location = LocationHelper.getInstance(mContext).getLastLocation();
            mLat = String.valueOf(location.getLatitude());
            mLong = String.valueOf(location.getLongitude());
            addGeofenceToMonitor(location);
            queryDb();
        }
    }       
}

Also here is where i add the pending intents and the geofence to the location client

addGeofenceToMonitor(Location location){
    List<Geofence> list = new ArrayList<Geofence>();
    list.add(getNewGeofence(location.getLatitude(), location.getLongitude()));
    PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, 
                                        new Intent(mContext,GeoFenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    OnRemoveGeofencesResultListener removeListener = new OnRemoveGeofencesResultListener() {

        @Override
        public void onRemoveGeofencesByRequestIdsResult(int statusCode, String[] requestIDs) {
            //To be used
        }

        @Override
        public void onRemoveGeofencesByPendingIntentResult(int statusCode,PendingIntent pendingIntent) {
            //Not used
        }
    };
    LocationHelper.getInstance(mContext).removeGeoFence(mGeofenceRequestIDs, removeListener);

    OnAddGeofencesResultListener addListener = new OnAddGeofencesResultListener() {

        @Override
        public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
            if(statusCode != LocationStatusCodes.SUCCESS){
                //handle error cases
            }
            else
                Log.i(TAG, "Successfully added Geofence "+geofenceRequestIds[0]+" for monitoring");
        }
    };
    LocationHelper.getInstance(mContext).addGeoFence(list, pendingIntent, addListener);
}

Here is the snippet from the manifest file

<service
android:name="com.myexample.sample.GeoFenceIntentService"
android:label="@string/app_name"
android:exported="true">
</service> 
Traxex1909
  • 2,650
  • 4
  • 20
  • 25
  • Have you tried using a `BroadcastReceiver` as the target of your `PendingIntent` instead of a `Service`? If not, please try that and see if `onReceive()` is called. – David Wasser Jan 27 '14 at 11:55

2 Answers2

0

Read this. Have you checked the position estimation circle you are getting? You can use mock locations app to set the position as well as the accuracy circle. Your geofence may be too small to accommodate your position circle and that is why the events are not triggered.

Community
  • 1
  • 1
Dilip
  • 1,122
  • 11
  • 31
0

Android GeoFences never enable the GPS (because their API is awful and their device power consumption is already so out of hand). You have to set up your geofences and then constantly poll the GPS separately if you want geofencing over GPS.

The handler of the GPS polling can be null, the poll only exists to force accurate information into their awful location API and in turn trigger the fences.

stealthwang
  • 915
  • 6
  • 8