好吧,这让我很生气。倒计时服务有限制吗?
我尝试了很多方法让倒数计时器在服务中工作,但总是失败。它永远不会到达 finish() 部分!
那么服务和倒计时有限制吗?请帮助我...
这是服务:
public class ss extends IntentService {
public ss() {
super("ss");
}
@Override
protected void onHandleIntent(Intent intent) {
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
executemymethod(); //it never gets here!
}
}.start();
}
}
如您所见,代码简单且正确,但仍然是 executemymethod();永远不会真正执行!没有错误...请给我一个解决方案!
最佳答案
如果你想做这种事情,你应该使用常规服务而不是 IntentService。这是文档对 IntentService 的描述:
Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
由于倒计时是异步的,因此在您启动倒计时计时器后工作就完成了。 IntentService 然后立即完成,并可能关闭线程。
关于Android 服务 + 倒计时 = 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8246545/