我对ScheduledExecutorService有些问题。我想在一段时间后开始通知。在这种情况下是10秒。但是10秒后它就不能启动了。除此之外,所有的功能都能正常工作。
代码如下:
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.schedule(new Runnable() {
public void run() {
Toast.makeText(BService.this, "It works", Toast.LENGTH_SHORT).show();
// Display a notification about us starting. We put an icon in the status bar.
showNotification();
}
}, 10, TimeUnit.SECONDS);感谢你的帮助Roa
发布于 2012-11-01 23:51:38
我也遇到了同样的问题,我找到了原因。UI线程不会调用run(),Toast会使该线程崩溃(但由于某种未知原因,它不会在LogCat上提供任何内容)。
但如果你这样做了:
task.schedule(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
// your Toas goes in here
}
});
}
}, DURATION, TimeUnit.MILLISECONDS);看起来不错!
发布于 2011-08-01 18:22:38
ScheduledExecutorService.schedule()的调用是正确的,所以我猜您的run()由于某种原因而出错了。您可以在 run()的开头添加一个日志,以检查它是否启动。
https://stackoverflow.com/questions/6862693
复制相似问题