I'm very new to Android programming so pls excuse my ignorance...
I'm trying to do simple Android app:
- User presses a button, starts postDelayed job and then waits on conditional var
after timeout the postDelayer job should signal
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 !"); }
What happens is that buttonClick() is stuck waiting and I'm not even seeing the "---> timeout notify" message after timeout...
What I'm doing wrong ?
EDIT: Tried to fix messed up example...