Android O, Background Service is running for more

2019-04-08 11:21发布

问题:

I am using FusedLocationProvider API to register some Geofences with PendingIntent, when my app gets started (app was manually killed to trigger this behavior) via Geofence event, I start a background service to do some work.

In this Service I create a separate background thread to do some long running tasks and I acquire a wake-lock so that I am sure my tasks are completed. The Service keeps running for longer period of times (30 - 50 minutes) even though It shouldn't be. It shouldn't be white-listed and keep itself running for longer period of times according to my understanding of recent changes in Android O.

Is it desired behavior or some kind of bug with PendingIntents waking up the app and then it's treated like white-listed even though those Intents are not for Notifications (i.e anything which user can see).

Or these intents falls into category of "Receiving a broadcast, such as an SMS/MMS message." as mentioned here in documentation

回答1:

It my be some bug in preview.

But, are you sure your service is running after 1 minute? Your app process and background thread you made will/could run even after service is destroyed.

So your service does pass onDestroy callback, but you don't stop your thread in there and continue doing work in background. It only stops when whole app's process is killed because Android decided it's not important any more.