Android geofencing when app is killed

孤人 提交于 2019-12-04 11:05:45

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.

  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.

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. :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!