Do Geofences Remain Active in Android After a Device Reboot

Do Geofences remain active in android after a device reboot

In my experience the geofences do not survive reboot. I use a BOOT_COMPLETED receiver just as you suggest. It works fine.

Are Android geofences surviving a reboot?

Geofences will not persist on reboot.

You have to listen for BOOT_COMPLETED action and add geofences again.

Note: If your app installed on external storage(SD card), you will never receive Boot Complete action. So you have to specify android:installLocation="internalOnly" in the manifest tag.
This is because, android device will broadcast BOOT_COMPLETED action before setting up the external storage.

Do Android Geofences remain active until removed/expired or only until my PendingIntent is launched

The good proposition is the first one. If you create a geofence with the flag NEVER_EXPIRE as expiration time, you won't have to re-register it when it is triggered (by going in or out). I'm 100% certain of this, I'm right now just finished coding and testing a POC about geofence.

From the doc, the only way for a geofence to be deleted is either expiration time is reached or it is deleted by the device itself.

Expiration time

How long the geofence should remain active. Once the
expiration time is reached, Location Services deletes the geofence.
Most of the time, you should specify an expiration time, but you may
want to keep permanent geofences for the user's home or place of work.

To stop geofence monitoring, you remove the geofences themselves

Android Geofence Automatically removed when location service is disabled / restarted

Found this Answer:::
http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html

In case network location provider is disabled by the user, the geofence service will stop updating, all registered geofences will be removed and an intent is generated by the provided pending intent. In this case, hasError(Intent) returns true and getErrorCode(Intent) returns GEOFENCE_NOT_AVAILABLE.

Now:::: How can we register the geofence back when the location service is back.. (IN BACKGROUND)

Geofence pending intent firing too late when device is in deep sleep (doze) mode

To improve it, Let's perform some checks

1) Use broadcast receiver to get it triggered easily instead of service. And set priority with intent-filter.

For e.g

    <receiver
android:name=".youpackage.GeoReceiver"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="yourpackage.ACTION_RECEIVE_GEOFENCE" />
</intent-filter>
</receiver>

And your pending intent will be :

Intent intent = new Intent("yourpackage.ACTION_RECEIVE_GEOFENCE");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
youractivity,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);

2) As your GPS goes in sleep mode, we need to wake it up while creating Geofence. Once you create your geofence, you can start pinging your GPS until you will get ENTER transition. This would must help to get triggering it.

public class GPSService extends Service implements GoogleApiClient.ConnectionCallbacks,    GoogleApiClient.OnConnectionFailedListener, LocationListener {

GoogleApiClient mGoogleApiClient;
LocationRequest locationRequest;

public GPSService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
super.onCreate();

mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

mGoogleApiClient.connect();
}

@Override
public void onLocationChanged(Location location) {

Utility.ReadAndWriteData(this, Utility.readFileName(this), "Still Geofence is not triggered!!!");

}

@Override
public void onConnected(Bundle bundle) {

locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setFastestInterval(1000);
locationRequest.setInterval(2000);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this);

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onDestroy() {
super.onDestroy();

if(mGoogleApiClient.isConnected()) {

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

}
}

And don't forgot to stop this service immediately when you get ENTER transition or It cause drain battery. This service is only to wake GPS up from sleep mode.

Re-register Geofences only when required

I found no way of detecting the case of Google Play services data is cleared, so I just request for new geofences whenever the app is opened.

As @Sagar mentioned, GEOFENCE_NOT_AVAILABLE is received in the transitions service. This is received when the Location is disabled. To workaround this, I save the geofences (using SharedPreferences) whenever I request them, and then I have a BroadcastReceiver that detects when the Location is enabled again. I reload the saved geofences here.

Thanks.



Related Topics



Leave a reply



Submit