7

I am trying to test Geofence functionality using Google's example: Creating & Monitoring Geofences. I have uploaded the code here.

Problem is that I never get a notification of entry or exit. I have Wifi, 4G and GPS on. I have tried to test in the following ways:

  1. I even walk out of my house for about 50ft and walk back in - but no notifications. I can see that play services gets connected and even the "GeofenceUtils.ACTION_GEOFENCES_ADDED in MainActivity.java" gets triggered, so I think Geofences are getting added correctly.
  2. Enabled Mock Locations in the Settings and used this Fake GPS App and changed location - starting from the same coordinates as the Geofence1 and then setting to something totally outside (in another state) - but I still dont get an exit notification.

What am I doing wrong? ANyone had success in running this Google's example: Creating & Monitoring Geofences. I have uploaded the code here for easy browsing.

I am just trying to learn Geofencing - detecting entry & exit. I WILL MARK ANY ANSWER AS THE RIGHT ANSWER THAT I CAN USE TO GET GEOFENCING WORKING ON MY REAL DEVICE

I enter the coordinates and radius below.

Androiderson
  • 16,055
  • 6
  • 62
  • 71
user1406716
  • 9,410
  • 22
  • 93
  • 151
  • 1
    Actually there's a bug in android sample code.you should not use IntentService class for notification and transition.Replace it with BroadCastReceiver. Same issue is posted here- http://stackoverflow.com/questions/19505614/android-geofence-eventually-stop-getting-transition-intents – Pankaj Apr 08 '14 at 06:02
  • Can you tell me how did you solve this issue ? – Hunt Jun 26 '14 at 04:44
  • I did not, and that is why I have not marked the below answer as the right answer. I need to revisit this at some point .... sorry. If you find a way, would be awesome if you can post a solution please. – user1406716 Jun 26 '14 at 21:48
  • @user1406716 Did you get the problem solved ? – Hunt Jul 20 '14 at 05:55
  • @Hunt No I did not have very good luck, I will probably come back to this in future. If I do, will update here. – user1406716 Jul 20 '14 at 07:17

4 Answers4

17

I was also having issues with the example code. One of the main issues I had was around declaring the ReceiveTransitionsIntentService. You don't need to add a BroadcastReceiver or request location updates, but you need to change your AndroidManifest to set the exported value to true, like so:

<service
    android:name=".ReceiveTransitionsIntentService"
    android:exported="true" >
</service>

Make sure that the path to the service is also correct.

I've simplified the Google example significantly and you can find the code and explanation here. I've removed the UI component and persistent Geofence storage, so this example should be easier to follow. Hope this helps!

mari
  • 211
  • 2
  • 4
0

Initally my code was running smoothy but after that I too get this problem , Please try this by modifying geofenceRequester class in continueAddGeofences() function. basically it is calling the receiver through location request method

 private void continueAddGeofences() {


            // Get a PendingIntent that Location Services issues when a geofence
            // transition occurs
            mGeofencePendingIntent = createRequestPendingIntent();

            // Send a request to add the current geofences
            mLocationClient.addGeofences(mCurrentGeofences, mGeofencePendingIntent,
                    this);
            //mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            try {
                LocationRequest locationrequest = LocationRequest.create();
                locationrequest.setPriority(locationrequest.PRIORITY_HIGH_ACCURACY);
                 locationrequest.setInterval(3000);


                mLocationClient.requestLocationUpdates(locationrequest, mGeofencePendingIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
Priya
  • 23
  • 3
  • I got the notification once but not able to get it again. How are you testing you app? Are you using a mock locations app or actually walking away from the geofence coordinates? How far are you walking away? – user1406716 Apr 07 '14 at 17:47
  • I am actually walking away from the geofence coodinates and walking away depends upon radius that you have given , I have given 10m radius – Priya Apr 08 '14 at 05:23
  • is a radius like 5m or 10m working for you? and as you walk in and out - you see separate notifications showing entry and exit???? – user1406716 Apr 08 '14 at 05:24
  • no, notifications replace the existing depends upon entering or exiting – Priya Apr 08 '14 at 05:28
  • @SpunkyPriya do i need to change something in BroadcastReceiver ? – Hunt Jun 26 '14 at 04:48
  • How are you testing this - on Genymotion or on a real device? I am not getting any results... – IgorGanapolsky Jan 26 '15 at 23:02
  • on real device @IgorGanapolsky – Priya Apr 01 '15 at 06:19
0

I was also having the same problem with Geofence api. At the end, I used addProximityAlert of LocationManager.It did work very well!

Eric
  • 426
  • 7
  • 11
-4

As you see, Google was reworked location logic, this is new API. Seems it work as "shared" location provider (LocationRequest#setFastestInterval can provide you coordinates if any other application requesting it, or you can receive coordinates with PRIORITY_NO_POWER flag).

Geofence API is like helper method, you do not need develop similar logic by itself (calculating radius, etc). But, Geofence does not request coordinates by itself, it just uses received coordinates (i.e. from other application). Method naming is self-explanatory: LocationServices.GeofencingApi.addGeofences; => addGeofences, not requestGeofences.

So, you should request coordinates using:

LocationServices.FusedLocationApi.requestLocationUpdates
Soma
  • 857
  • 2
  • 17
  • 32
Niobij
  • 95
  • 4
  • 1
    This is not correct, you do *not* have to requestLocationUpdates for the system to send you geofence intents. – akent Oct 21 '15 at 23:06
  • @akent if I don't request location updates I'm not receiving geofence intents from the system but if I open any app that request locations (like a map) or I just turn on location updates from my app, it starts triggering. Are you sure about you stated? how did you make it work? – Juan Saravia Mar 02 '17 at 21:39