Runnable r
每分钟定期触发一次handler.postDelayed(60*1000)
Runnable r
。我在真实设备上测试我的代码。
但是我注意到,当手机正在充电(AC或USB)时,它运行良好,但如果手机断开连接,private final Runnable listen_to_server = new Runnable(){
public void run(){
new Thread() {
public void run() {
try {
handler.sendMessage(handler.obtainMessage(PING_SERVER,"listen"));
synchronized (listen) {
listen.wait();
}
handler.postDelayed(listen_to_server, 50000);
}
catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
};
只会每20分钟运行一次,甚至更糟,几个小时。谢谢你的帮助!
以下是代码:
handler=new Handler() {
@Override
public void handleMessage(final Message msg) {
switch (msg.what) {
case PING_SERVER:
String type = (String) msg.obj;
add_log("ping server"); // print to screen and Log.d
...
...
}
}
}
在处理程序中:
typedef enum
{
FSM_State1 = (1 << 0),
FSM_State2 = (1 << 1),
FSM_State3 = (1 << 2),
FSM_StateError = (1 << 3)
} states_t;
答案 0 :(得分:1)