Is it a coincidence that my service is killed almost always after Thread.sleep()? There are other ways for making an android service "unkillable"?

42 views Asked by At

I have an app with a service that run in the foreground and perform some tasks. Of course whenever the system believe there is little memory available it kill my service. By looking at the logcat I've noticed that the service is almost always killed when it's waiting, meaning during the invocation of Thread.sleep(). Is that merely a coincidence or is it something that I could address? Would it help to maybe call System.gc() before the Thread.sleep()?

Of course there are also the workarounds of making my service "unkillable". Please bear in mind that my device has root permissions with Magisk. By googling I came across mainly of two ways in achieving that:

  • Moving my app as system app and using the android:persistent="true" in the manifest file.
  • Making another app with a Broadcast Receiver in order to restart the service when it gets killed by the system.

Not sure though if I like these two solutions, because in the first case scenario I would need to move my app as system app and the second one requires the install of a second application.

What do you suggest? There are other workarounds?

0

There are 0 answers