Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

android - How to keep service running after force-stop?

I've seen that if you kill some process, it restarts immediately and keep running. How this implemented? How to inform system that my service should not be killed and if that happened - restart it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Android just does it. As an OS, Android is specifically designed to run with ram full at all times. So if you kill, or an app force closes it will, if their is room in ram for it be restarted by Android to fill the ram back up.

It does this because even the "fastest" phones are snails to even the most average of desktops and keeping as many programs that you use loaded in ram as possible enables it to simply "resume" the program instead of having to go through the slooooow, time wasting process of having to reload it back into ram and then running it from the beginning.

Android kernels have their own task manager. This means that it will be more efficient than any app-based task manager as it is run at the kernel level, and it should be left up to that task killer to decide when to free up memory or not free up memory. Let Android do what it was designed to do. Anything you do to try and force it to rerun a program or stop a program will, in the long run, slow it down more and possibly even cause stability issues.

However the short answer to your question is that there is no way to tell the "system" that your process should not be killed or to restart when it is closed. That is a choice made entirely by the kernel level task manager.

BTW, why would you want to? I ask this because I don't think you have thought this through very well. Remember, unlike IOS owners who are used to and expect total control over their device to be in the hands of Apple(for good or ill) Android owners expect, and will have control over their device. If you try to take that away from them, you will likely find most people uninstalling your service. And demanding their money back if you charge a fee for it.

I hope this has helped.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...