我对Android编程很陌生,所以请原谅我的无知...
我正在尝试做简单的Android应用程序:
- 用户按下按钮,启动 postDelayed 作业,然后等待条件变量
超时后 postDelayer 作业应该发出信号
private final static long TIMEOUT = 10000; private Handler mHandler; final Lock lock = new ReentrantLock(); final Condition condition = lock.newCondition(); @Override protected void onCreate(Bundle savedInstanceState) { ... mHandler = new Handler(); ... } private void timeOutSignal() { mHandler.postDelayed(new Runnable() { @Override public void run() { Log.d(">> ", "---> timeout notify"); lock.lock(); try { condition.signal(); // releases lock and waits until doSomethingElse is called } finally { lock.unlock(); } } }, TIMEOUT); } public void buttonClick(View view) { timeOutSignal(); Log.i("???", "... WAIT"); lock.lock(); try { condition.await(); } catch (InterruptedException e) { // todo } finally { lock.unlock(); } Log.i("???", "... WAIT DONE !"); }
发生的情况是 buttonClick() 一直在等待,超时后我什至没有看到“---> timeout notify”消息......
我做错了什么?
编辑:试图修复混乱的例子......