Contact Android Not a few days, not quite understand.
An application that was originally written was inadvertently discovered, and when it was on standby, one of the threads in the application stopped running.
This thread is uploading a data to the server every other minute.
When I was testing, I did not think about the situation under the standby (the key), now found that as long as the phone into the standby state, the thread will stop working.
But there is a strange phenomenon, because my application started three threads at the same time.
One is responsible for uploading a data every minute, when the standby, the thread is suspended, when the phone is not standby, immediately resurrected.
A UDP packet that is responsible for receiving the server, the thread is not affected by the standby, when there is data, it can be handled normally.
Is it because of datagramsocket.receive (datagrampacket), the cause of the blockage?
public void Run ()
{
while (true)
{
Datagramsocket.receive (Datagrampacket); Blocking
}
}
To search the Internet for a moment to see what others said: http://www.cnblogs.com/jqyp/archive/2011/06/14/2080543.html
Lab 1: Using Java.util.Timer
When connected to the USB cable for debugging, it will find that everything is working properly, update the interface every 5 seconds, even if the power button is pressed, will still be triggered 5 seconds.
When the USB cable is unplugged, press the Power key to turn off the screen, after a period of time to open again, found that the timer obviously did not continue to count, stuck in the power button off the number.
Lab 2: Using Alarmservice:
2.1 A broadcast is sent by Alarmservice each 5 seconds, and the setrepeating type is alarmmanager.elapsed_realtime.
Unplug the USB cable, press the power button, open the screen again after a while, and notice that the timer does not continue to count.
2.2setRepeating Yes type set to Alarmmanager.elapsed_realtime_wakeup
Unplug the USB cable, press the power button, turn on the screen a little longer, and find that the timer is counting.
In this case, the use of wakeup to ensure that the timer you want to work, but it will certainly cause increased power consumption.
I finally wrote a service class, and then I used Alarmservice to run every other minute, and it worked when I was on standby.
2011-11-26
Android: How to keep the program running extends Service while on standby