When you call wait()
inside a class, you are actually calling wait()
on the current instance of that class. To do that, it requires a lock on that instance. If another thread has the lock, you will not be able to wait.
Adding synchronized
to onCreate()
will acquire the lock (the current instance of the Activity
) and let you call wait()
inside, but this is definitely not want you want to do as it will block the main/UI thread and cause your app to be non-responsive.
What you probably want instead is the following inside your activity:
private boolean isTerminationConditionMet = false;
@Override
public void onCreate() {
//standard Android onCreate code
final Handler handler = new Handler();
final Runnable task = new Runnable() {
@Override
public void run() {
//code you want to run every second
if (!isTerminationConditionMet()) {
handler.postDelayed(task, 1000);
}
}
}
handler.postDelayed(task, 1000);
}
This will cause the code inside run()
to be run after the desired delay (1000ms) and then every 1000 ms until your termination condition is met.