Not seeing Geofence Transitions in Google's tu

2019-03-27 10:32发布

问题:

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

.

回答1:

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!



回答2:

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();
            }


回答3:

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



回答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