timer = new Timer("Timer Thread");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
showDialog(0);
timeBar.setProgress(time);
}
}
}, INTERVAL, INTERVAL);`
我的 onCreateDialog 方法工作正常,所以当我从 Button 使用 showDialog(0) 时它工作正常。但如果该方法由调度程序调用则不是。
最佳答案
使用处理程序:
protected static final int DIALOG_OK= 0;
timer = new Timer("Timer Thread");
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
mDialogHandler.sendEmptyMessage(DIALOG_OK);
timeBar.setProgress(time);
}
}
}, INTERVAL, INTERVAL);
private Handler mDialogHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case DIALOG_OK:
// We are now back in the UI thread
showDialog(0);
}
};
};
这允许您从其他线程调用 UI 线程上的方法。需要更多解释,尽管问。
关于如果从计时器任务调用,Android 对话框根本不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5292141/