I have a Service running, that opens a port and receives messages. It does so by opening a port and listening to it in an infinite loop. When the screen is off for a few minutes, I don't receive any messages anymore.
Right now, I try to stay awake by aquiring a WakeLock. But this doesn't seem to work and consumes a lot of battery. So how can I keep the infite loop running and maybe how do I achieve it with minimum battery consumption.
This isn't the real code, but this is how I try to achieve staying awake in standby mode.
MyService{
...
public void startListening(){
new Thread(new Runnable(){
public void run(){
PowerManager mgr = (PowerManager)c.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ListeningLock");
new Thread(new Runnable(){
public void run(){
wakeLock.acquire()
someClass.infiniteListeningLoop();
wakeLock.release();
}
}).start();
}).start();
}
}
Update
The WakeLock is not the problem. I periodically make a notification that the loop is still running and how many times it already ran. So the problem seems to be the socket. I catch all Exceptions, but there is none thrown. May Android close the port after a while of not receiving anything?