Persistent service
Asked Answered
D

2

18

I know that some peoples view is that you should not create persistent services.

But for those of us that want to increase our knowledge, how does one go about creating one in theory?

I have been playing around, and noticed that services that use startForeground are killed in low memory situations.

I have seen a, now deprecated, permission PERSISTENT_ACTIVITY, but that didn't do much.

So what is the secret?

Dipetalous answered 16/1, 2011 at 23:27 Comment(0)
S
51

You can only make your service persistent if you are developing system apps. These services will be basically un-killable, and are labeled as "PERS" in the output of the "adb shell dumpsys activity" command. It's not very well documented, so I did a quick write-up on Persistent services in Android...hopefully it will help some people out.

But for non-system apps, startForeground is your best option.

Selfseeker answered 28/2, 2011 at 17:49 Comment(5)
Thank you for this - I have being searching for a solution for days - Its good to know there are some people on StackOverflow that know there are system app developers out there.Butterbur
Additional Note: The persistent flag works perfectly - the service is NEVER killed, even when the device is seriously low on resources.Butterbur
There is a bug in KitKat 4.4.2 which stops services without prior notice, if Google continues this bug we should make our App Widgets set to PersistentCommence
@Andrew, any idea about application update for persistent and system app?Virgina
I am working in system application and i have enable android:persistent="true" flag as well in this project. I have created service which will be doing some periodic tasks but when the phone is lock then the handler runnable is not called inside the system app service but when phone is unlock or in charging mode then the handler runnable is working fine. May i know why this is happened and what is best solution to make periodic task inside system app service. Android version 'android-11' Thanks in advance.Hortenciahortensa
A
-8

There is absolutely no way of preventing the OS from killing your service.

Audit answered 17/1, 2011 at 0:16 Comment(12)
see, i am not 100% sure that is correct, when I do "adb shell dumpsys activity" I do see some services labeled as PERS ... I assume that is persistent ... how did they get that label?Dipetalous
Do you even know what that label means? Android is free to kill anything at any time. Some things have higher priority than others, but you can't prevent it. What is your service trying to do that that you think it's so important to keep around?Audit
So I am not sure what the label means, that is why I wrote that I "assume" it means persistent. So what do services that play music do? Do they risk being randomly shutdown? I would assume, again, that this is a background event, right?Dipetalous
A service will only be killed if the system is low on memory. Music players are free to be killed by the system. There is a "startforeground" method in a service which gives it higher priority, it might be deprecated though. But again, it's still up to the system.Audit
yeah, the setForeground/startforeground makes it sound like you wont be killed, but that isnt what I found.Dipetalous
The downvote is for using the word abosuletly when there is way as stated in the selected answer. That method works, but again you have to be a system developer. And, for the sake of knowledge (like the question states) it is correct. For most apps and purposes it is impossible.Sipes
@Shaun: Thought I haven't specifically looked at the android source related to the persistent tag, I can pretty much guarantee you that even those are not 100%, completely unkillable. If the phone has a choice of killing a persistent service or rebooting, I'll bet it would still choose to kill (and then restart) the service. I'd love to see the source related to this tag though.Audit
Persisten tags are unkillable unless your on some modded android OS. That is why not all apps are written, or can be written as system. You would need root access to kill a system service. System services are things such as the system alarm clock, sms listener, bluetooth, etc. The system will not dump these as they are system critical.Sipes
If a service is always restarted when it is killed, I think that's what most people would call "unkillable".Minima
-1 Please don't make 'absolute claims' without considering system developers first.Butterbur
@ChrisNoldus: Unless by system developer you mean linux kernel developer, then my answer stands.Audit
@Audit There are lots of people who develop apps for custom devices - such developers (like myself) can sign their apps with the android-system key thereby having the ability to create System Apps. Such people need know nothing other about other languages, or the Linux system (You can make System apps using Eclipse in Windows). As I said before - don't make absolute statements which can not possibly be true (Someone must be able to make System Apps). This site is for developers at every level.Butterbur

© 2022 - 2024 — McMap. All rights reserved.