0

I'm fighting with the Android desire of killing everything which isn't active on the screen. My problem in few words:

  • I have a microcontroller which communicates with a processor on which Android runs;
  • the processor must keep active a watchdog on the microcontroller, resetting periodically (every one second) one of its registers; an application, say App B, accomplishes this duty;
  • on the processor I can be sure about the persistent existence of another application, say App A (or, however, if App A dies App B can die too because the system is compromised) which for now does nothing, in the future will accomplish other duties.

Which is the best way to implement App B?

I tried the following solution: App B contains a Bound Service, say Service A, to which App A can bind on; Service A starts a thread, say Thread A, which periodically resets the microcontroller watchdog. Thread A is launched when app A sends a command to Service A (e.g. START_WATCHDOG).

In my idea, Service A lives until App A lives (thanks to the binding), and so the process to which Service A belongs lives, and so also Thread A.

Unfortunately, from tests I see that sometimes (in a sporadic manner), after some time (sporadic time, too), with almost no work running on the system (except for App A, Service A and Thread A) the system kills Service A process, and so Thread A stops and the watchdog elapses.

When Service A dies, it is restarted (because it is a Bound Service and App A is still running) but, for now, I don't save the current state of Service (which simply consists on the START_WATCHDOG command arrival or not) and this is the reason for which the watchdog elapses.

So, I've got several questions about my solution:

  • is it ok and I simply need to save the current state of Service A in order to restore it when restarted?
  • should I discover better the reasons for which Service A, or better its process, is killed?
  • is there a better solution for my problem?

Thank you very much to everyone who will spend some time to help me.

ema_anfu
  • 1
  • 1
  • 3
  • https://developer.android.com/training/run-background-service/create-service. Did you go through this? – Vinay Prajapati Aug 21 '19 at 10:44
  • try foreground service instead of just a regular one... that is the least likely to get killed and probably the only solution to your current problem. I have a similar battery monitoring service for my app (my stupid phone does not give low battery warnings so i made a service... lol) anyway, so far it has never been killed... been over a month.... it is still possible but not very likely for it to get killed, cons -> sticky notification – Kushan Aug 21 '19 at 10:47
  • As @Kushan said, Foreground service is the solution for you problem. – Furqan Khan Aug 21 '19 at 10:52
  • Does this answer your question? [Continually Running Background Service](https://stackoverflow.com/questions/51289236/continually-running-background-service) – Ryan M Mar 31 '21 at 06:14

2 Answers2

0

Being not sure about periods in which your service runs you can try these:

  • Use foreground service. However, you might need to acquire a wakelock within your service start point if you need cpu in long time. Plus, a notification needs to be shown on phone status bar.
  • Use WorkManager-new api part of jetpack simplifying the use of alarm managers and jobschedulers- to schedule your tasks periodically. However if your frequency is higher than 1 per 5-10minute then you will need to take care of doze mode. If phone gets into doze, your tasks might be delayed till maintenance periods. A trick to apply here might be starting a foreground service when you catch activation of doze mode and return back to Workmanager logic in deactivation(if you don't want user to see the foreground service's notification). Do whatever you want in the foreground service like.
  • Use Firebase Cloud Messaging to push notification from your server to your users periodically for you to have a small amount of time to do work in background. When notification comes, OS grants you an interval to run a task.
Mertcan Çüçen
  • 466
  • 3
  • 13
0

Use Work manager it is easy to implement.