I have a MyService
class which contains a nested BroadcastReceiver
class:
public class MyService {
private Object lock;
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
synchronized(lock) {
doTask();
lock.wait();
Log.d(TAG, "After wait ...");
}
}
}
private synchronized void doTask() {
Log.d(TAG, "do task ...");
synchronized(lock) {
lock.notify();
}
}
}
As you see above, when onReceive()
is called, my code run doTask()
in a synchronized
block. Then, in doTask()
function, my code run lock.notify()
. I expected to see the log in terminal Log.d(TAG, "After wait ...");
I run my Android project. When onReceive() is triggered, I can see the log "do task ...", then, it is hanging there, I can't see log "After wait ...", why my wait-notify is not working as expected?