简体   繁体   中英

How can I create an android service that dalvik will not kill?

Before I begin my question, I want to preface this with: I KNOW IT'S A BAD IDEA TO FORCE A SERVICE TO RUN FOREVER... I simply do not care.

This application is not for consumer use. It is not going on the app store. It is for my use only.

Alright, well I have this unused HTC Sensation running 4.0.3 (ICS) sitting around, and I have volunteered it to a local theatre for a task. The task is for it to ring on cue whenever it is needed in the show. We don't want a sim card in it because someone might accidentally call the phone during the show when it is not supposed to ring.

So I created a fake phone application that receives a signal via TCP from a server that I have set up to send signals to devices over the LAN. Right now I have the listener running in an infinite loop in a service. I am, however, still experiencing the service not responding to the TCP signals.

I would really appreciated it if some android guru's could give me some hints/tips for making this service as reliable as possible, good/bad coding techniques aside I want to do everything possible to make this service unkillable. This phone has only one job now, and that is to always be listening for incoming messages, no matter what.

Things I have done so far:

Created a Service (and launched a separate thread from that service) Used startForeground(id, notification); Activated DeviceAdmin and created a wakelock

anything else you guys can think of?

There is no way to ensure that Android will never kill a service. If you make it a foreground service it reduces the odds, but you can't insure it.

First idea that pops into my mind would be setting up an AlarmManager that checks every 5 seconds whether or not the service is running. This describes a method to see whether a service is running . And if the service is not running you can just restart it.

Using this and the startForeground()-method may work.

Kind regards

Based on this quote from the Service javadoc...

Other application components running in the same process as the service (such as an Activity) can, of course, increase the importance of the overall process beyond just the importance of the service itself.

... if you leave the activity that starts your service in the foreground, that will pretty much guarantee that the Service won't be stopped [I posted a comment starting to suggest this idea that may have been cut off.]

To add another level of reliability, hold a wake lock in your activity, using this doc as a guide: https://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Example code:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
// ..screen will stay on during this section..
wl.release();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM