I used an Android tutorial with Geofences, which obviously does not work when the application is closed. So, after searching, I noticed that the b-ryce user on SO used BroadcastReceiver, so geofences are launched even if the application is not active ( link for his SO question ).
I just can't get geofences to fire when I move outside / inside a registered location. Here is my procedure:
mGeoLocation = new GeoLocation(sInstance);
mReceiver = new Receiver();
sInstance.registerReceiver(mReceiver, mReceiver.createIntentFilter());
And inside the GeoLocation class:
protected PendingIntent getTransitionPendingIntent() {
if (mGeoPendingIntent != null) {
return mGeoPendingIntent;
}
else {
Intent intent = new Intent(getClass().getPackage().getName() + ".GEOFENCE_RECEIVE");
return PendingIntent.getBroadcast(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
This is how I create a new geofence:
Geofence fence = new Geofence.Builder()
.setRequestId(hashCode)
// when entering this geofence
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(
Double.parseDouble(single.getSetting("latitude")),
Double.parseDouble(single.getSetting("longitude")),
Float.parseFloat(single.getSetting("radius")) // radius in meters
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
mGeofences.add(fence);
The array is filled, and we call the AddGeofences method inside the Geofence class
mGeoLocation.AddGeofences(mGeofences);
AndroidManifest.xml for the receiver class:
<receiver android:name=".Receiver" >
</receiver>
And the Receiver class should just register when geofence triggers
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("sfen", "Broadcast recieved "+ action +" but not needed.");
}
, Geofence , , . ( ) , .
- , ?