作者PCIT (冈山的火车  N I
看板AndroidDev
标题[问题] 从Activity设service thread的参数
时间Thu Feb 9 23:43:40 2012
大家好,我有一个IPC的问题想请教有经验的板友
我有一个Activity,里面有几个buttons,用户可以按其中一个开始service
这个service会启动一个thread。然後thread的run method 有一个while loop like
while(!Thread.currentThread().isInterrupted())
{
poll data from web
process data
send notification
}
如今,我想要让用户可以改变send notification里面的内容。例如,
让用户自行决定message要不要振动。
所以,我想把我thread里面的while loop用Looper class改写。然後用message queue
来决定要不要设这些attributes
while(!Thread.currentThread().isInterrupted())
{
Message msg = mq.dequeue()
check_msg(msg);
long poll data from web
process data
send notification
}
不过这个问题就在於,mq.dequeue()是block operation,如果queue是空的,
它会block到有东西为止。不过我的情形是要一直做同一件事(pull data from web),
直到用户要不同的资料再用message queue来沟通。
所以我想到的解决方法有两个,不知道哪个比较好?还是都不好XD
1.)在while loop的最後,新增一个sendEmptyMessage(0),所以至少有一个message
在queue里面。
2.)register message idleHandler。如果queue是空的话,在callback里
sendEmptyMessage,return true,所以这个handler会一直active。
请问我该用哪种方法好呢?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 72.201.78.127
1F:推 weruioi:有HandlerThread可以直接用 不需要自己写 02/11 02:18
2F:→ weruioi:Handler建立方法则为new Handler(thread.getLooper()); 02/11 02:19
3F:→ weruioi:印象中在ApiDemo里的某一个Service范例就是这种作法 02/11 02:21
4F:推 tomap41017:回楼上:IntentService 02/11 22:53