当我触摸显示屏超过 5 秒时,我正在尝试创建延迟操作。我为此使用了一个 Handler 和一个 Runnable,使用handler.postDelayed(runnable, 5000);
我还想要一个 ProgressBar 来显示 Handler 何时启动。从研究中我发现,我必须覆盖该handleMessage()
方法..这就是我尝试过的。
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progress.setProgress(progress.getProgress() + 100);
sendEmptyMessageDelayed(0, 100);
}
};
private Runnable runnable = new Runnable() {
public void run() {
playAlarm();
}
};
...
progress = (ProgressBar) findViewById(R.id.progressBar1);
...
@Override
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Execute Runnable after 5000 milliseconds = 5 seconds.
progress.setProgress(0);
handler.postDelayed(runnable, 5000);
mBooleanIsPressed = true;
}
if (e.getAction() == MotionEvent.ACTION_UP) {
if (mBooleanIsPressed) {
mBooleanIsPressed = false;
progress.setProgress(0);
handler.removeCallbacks(runnable);
}
}
return true;
}
它没有崩溃。但是 ProgressBar 根本没有显示任何东西。