Android geofencing when app is killed

2019-04-09 21:27发布

问题:

My main requirement would be to have a service having its own process and trigger its own geofencing event. I'd like the user to be notified from the notification center when he enters a geofence, even if the app is killed.

I read about services and this seems pretty clear to me, the Android documentation is dense so I managed to understand how to start a service with its own process, and also how it is able to communicate with the app using Messenger.

Then there is this code sample from Google showing how to use geofencing with google play services: Google samples geofencing

What I found so far is that we have to use an IntentService to trigger geofencing events, and from the docs I've read it states that an IntentService terminates itself when its work is done. And I tested a bit also, it looks like the Intentservice gets killed when the app gets killed, which doesn't surprise me regarding the documentation.

So my question is, how could we perform a geofencing service having its own process? Is that possible to avoid using IntentService for this matter?

EDIT:

The reason I wanna do this, is that the user can set geofence "reminders", it is based on a daily usage. For instance imagine each day you arrive at your workplace, we should be able to fire an alarm (or a notification) when we enter this geofence regarding the app is running or not. We don't want the user to worry about having the app running in background.

回答1:

  1. You don't have to use separate process for this purpose (unless you have other features in mind which you didn't say here loud)
  2. Use WakefulBroadcastReceiver which is indeed perfect for handling geofence transition events.


回答2:

I actually realized this by creating a service with its own process. This service is bound with IPC communication, my activity then registers to it when needed.

Within this service, I did apply the geofencing example that I found in google samples. It uses an IntentService to trigger geofencing events. Instead of using this logic in my activity, I use it in my own service. Now I can receive notifications even when my app is killed.



回答3:

You are right about Intentservice ,It gets killed as it completes its task. But there is a work around that can get you service working even after your app is killed.

Do remove following code from you Activity where you mentioned location code.

Remove mentioned code from you activity

check your onPause()

@Override
protected void onPause() {
    super.onPause();

    //Comment out below code 
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
}

check your code in onStop()

 @Override
protected void onStop() {
    super.onStop();
    //Comment out the below code if you have done in your activity to disconnect from google api client.

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

Put some log in your Intentservice ,You will see that when you run your app after mentioned modification you can even get location update even after you app is killed. :-)