Not seeing Geofence Transitions in Google's tutorial on Geofencing
Asked Answered
O

3

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.

Otiliaotina answered 7/4, 2014 at 6:35 Comment(5)
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- #19506114Coverdale
Can you tell me how did you solve this issue ?Montpellier
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.Otiliaotina
@Otiliaotina Did you get the problem solved ?Montpellier
@Montpellier No I did not have very good luck, I will probably come back to this in future. If I do, will update here.Otiliaotina
H
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!

Hemicellulose answered 8/8, 2014 at 18:55 Comment(5)
Hey your blog is really helpful, thanks. I think there's an error in there, however - in the manifest section it lists <service .. android:exported="false". I think you mean true. Thanks!Quimby
@Hemicellulose I don't understand what different this could possibly have?? I mean, I've tried it and no dice.Commove
This is a good answer... making android:exported="true" fixed the issueIvoryivorywhite
I agree with other posted. This is the correct answer. Android Studio puts android:exported="false" when creating the IntentService. You have to go in the Manifest and change it to true. This answer should be marked as the solution.Pedi
Brilliant. Thank you very much @HemicelluloseSpic
D
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();
            }
Deferment answered 7/4, 2014 at 7:7 Comment(7)
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?Otiliaotina
I am actually walking away from the geofence coodinates and walking away depends upon radius that you have given , I have given 10m radiusDeferment
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????Otiliaotina
no, notifications replace the existing depends upon entering or exitingDeferment
@SpunkyPriya do i need to change something in BroadcastReceiver ?Montpellier
How are you testing this - on Genymotion or on a real device? I am not getting any results...Commove
on real device @CommoveDeferment
B
0

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

Buttonhook answered 6/11, 2015 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.