2

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?

the_camino
  • 356
  • 5
  • 18
  • http://stackoverflow.com/questions/3856767/android-keeping-a-background-service-alive-preventing-process-death But it isn't guaranteed? Android knows this isn't something that should be done on consumer devices so they don't really support it. – zgc7009 May 29 '14 at 18:23
  • I am currently using startForeground(). I was hoping for any other settings that I can use. – the_camino May 29 '14 at 18:54
  • Why does it need to be a service? For the purpose of a fake phone call app that only needs to fool people in the audience, an activity will do, and you can leave it running in the foreground. – x-code May 29 '14 at 20:03
  • @x-code - Your comment got a little cut off, but in guessing what you were asking, it needs to be a service because the app needs to be launched remotely at arbitrary times over the course of a two hour show. – the_camino May 29 '14 at 20:05
  • @user Sorry, I fat fingered the post button in mid-sentence. Should be fixed now. – x-code May 29 '14 at 20:07

3 Answers3

2

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I believe I have done that by calling the method "startForeground()" in the "onStart()" method of my service. Unless you are referring to something else? – the_camino May 29 '14 at 18:50
2

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

Community
  • 1
  • 1
LorToso
  • 286
  • 1
  • 9
  • This is a good idea. I can check for a few other things too. The only issue is re-establishing the TCP connection since the server won't know the phone has disconnected and it may not be waiting on the socket.accept() method – the_camino May 29 '14 at 19:50
  • Is AlarmManager a permanent process? i.e. Can I always count on it to run? – the_camino May 29 '14 at 19:57
  • The AlarmManager should be permanent, but stops your receiver when the application is closed. You could check this Thread, that has a similar problem to yours. http://stackoverflow.com/questions/16274988/using-an-alarmmanager-to-check-periodically-that-my-permanent-service-is-running – LorToso May 29 '14 at 20:27
  • If I can get this to run and check constantly every 1 or 2 seconds. Then, with some finagling of the TCP server (making it state based, rather than persistent) Then the AlarmManager should make a beautiful safety net, thank you – the_camino May 29 '14 at 20:34
0

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();
x-code
  • 2,940
  • 1
  • 18
  • 19
  • Thanks for this, I believe I do leave it in the foreground. I'm fairly new to android, but I believe the activity remains in the foreground even after the screen is locked, so long as no other activity is accessed correct? – the_camino May 29 '14 at 20:25
  • No, that's a good point, because the lock screen becomes the foreground activity if the device locks. You can prevent the device from locking by holding a wake lock. – x-code May 29 '14 at 20:31