addProximityAlert doesn't work as expected
Asked Answered
M

1

3

ios sdk has great region monitoring functions. I need something like that in android and i think we have two alternatives. Geofencing and LocationManager.

Geofencing has really tidy examples and bugs , so i prefered LocationManager. Everyting works fine in LocationManager except one. If you add your current location as ProximityAlert , it immediatly fires "ENTERING" , but it is my current location , it doesnt mean that i entered this region. Because of that , it fires "ENTERING" each time i start my application if i am in region.(Even if i am not moving)

How can i solve this problem and fire event only if user is really ENTERING the region ?

Here is how i am adding PeddingIntents for my locations.

    LocationManager locationManager =  (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);

    for(Place p : places)
    {
        Log.e("location", p.location);

        Bundle extras = new Bundle();
        extras.putString("name", p.displayName);
        extras.putString("id", p.id);
        Intent intent = new Intent(CommandTypes.PROX_ALERT_INTENT);
        intent.putExtra(CommandTypes.PROX_ALERT_INTENT, extras);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,Integer.parseInt(p.id), intent,PendingIntent.FLAG_CANCEL_CURRENT);
        float radius = 50f;
        locationManager.addProximityAlert(p.lat,
                p.lon, radius, 1000000, pendingIntent);

    }       

Receiver

public class ProximityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    final String key = LocationManager.KEY_PROXIMITY_ENTERING;
    final Boolean entering = intent.getBooleanExtra(key, false);

    Bundle b = intent.getBundleExtra(CommandTypes.PROX_ALERT_INTENT);
    String id = b.getString("id");
    Log.e("here" + id, "here");

    if (entering) {
        Log.e(TAG,"entering");
    } else {
        Log.e(TAG,"leaving");
    }
} 

Manifest

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

Thank you very much

PS: iOS does not have this problem and their documentation explains it so

Monitoring of a geographical region begins immediately after registration for authorized apps. However, do not expect to receive an event right away. Only boundary crossings generate an event. Thus, if at registration time the user’s location is already inside the region, the location manager does not automatically generate an event. Instead, your app must wait for the user to cross the region boundary before an event is generated and sent to the delegate. That said, you can use the requestStateForRegion: method of the CLLocationManager class to check whether the user is already inside the boundary of a region.

Meritocracy answered 6/12, 2013 at 12:11 Comment(1)
If you look at the source code locationManager.addProximity(..), then you will see this method work with geofence.Underthrust
E
3

EDIT: since i wrote this, there has been a new thing added to the geofence API, 'setInitialTrigger' that allevates this:

https://developers.google.com/android/reference/com/google/android/gms/location/GeofencingRequest.Builder#setInitialTrigger%28int%29

Yeah, this is a nuisance, and is one of the major points where Android and IOS geofencing differs, unfortunately.

Android alerts when you're inside the Geofence if it knows you were outside before OR you added the geofence while inside it.

The way i solve this is with a 'grace period' in my broadcast reciever. Basically, when i create the Geofence, i store away its creation time in sharedpreferences, and i check against that value in onReceive.

By doing this, any 'immediate' hit will be filtered away. Perhaps 3 minutes is too long for someone else, but it works for me based on how i work with the geofences in my app.

private static final Long MIN_PROXALERT_INTERVAL = 18000l; // 3 mins in milliseconds

...

long geofenceCreationTime = session.getPrefs().getCurrentGeofenceCreation();
long elapsedSinceCreation = now - geofenceCreationTime;
if(elapsedSinceCreation < CREATIONTIME_GRACE_PERIOD){
        if (ApplicationSession.DEBUG) {
            Log.d(TAG, "elapsedSinceCreation;"+elapsedSinceCreation+";less than;"+CREATIONTIME_GRACE_PERIOD+";exiting");
        }
        return;

    }

Hope you see what i'm getting at.

Hope it helps.

Expositor answered 29/1, 2014 at 16:38 Comment(2)
Just add an extra with SystemClock.elapsedRealtime() to the pending intent, then compare with that when you receive the broadcast. No need to store as a preference.Blok
Well, i think there might be a reason. If the phone is restarted, and you want to re-create the geofence, you will want to keep track of the original creation time of the geofence so that you can set a proper timeout of the recreated geofence. Say you want it to be 12 hours, and the phone is restarted after 4 hours. You then need the original creation time to be able to set the timeout of the "new" geofence to 8 hours. This is why i stored it in preferences anyway.Expositor

© 2022 - 2024 — McMap. All rights reserved.