Android Question Staying Alive : where to code PartialLock

3fazeac

Member
Licensed User
Longtime User
I'm trying to keep my app, or at least the service it spawns, alive - at least overnight. My phones (Honor 8x (android 9)) is very good at killing off services and applications. As per tutorials I've used phoneWakeState.PartialLock and also startServiceAt along with Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER followed by Service.StartForeground(nid, CreateNotification("..."))

My app comprises a 'start' Button in the Main GUI which starts a service (started in foreground mode, with a notification) and this service, every 10 minutes, starts a temporary 'worker' that happens to talk to a bluetooth controlled relay. This worker service kills itself off after every call (it runs less than a minute).

My question is, where should the phoneWakeState.PartialLock be coded? In the Main or in the foreground service? Or both?

My experiments show that startServiceAt is largely ignored as the phone sleeps and I see that Erel's GPS example has removed it. (I think - haven't checked recently)

I have had some luck putting the PartialLock code in the Activity_Resume of Main. I release the PartialLock in Main.Pause - but only if UserClosed is true.

Putting the PartialLock in *only* the foreground service doesn't always keep the service running while the phone is sleeping.

Should I *also* code in the PartialLock in the foreground service (as well as Main) to be extra sure my app wont be killed off while sleeping?
 
Top