can anyone tell me why in android.developer documentation they said : just sleep for 30 seconds.
and then put 15 * 1000, which is not 30s it’s only 15 !!
Runnable mTask = new Runnable() {
public void run() {
// Normally we would do some work here... for our sample, we will
// just sleep for 30 seconds.
long endTime = System.currentTimeMillis() + 15*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (mBinder) {
try {
mBinder.wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
// Done with our work... stop the service!
MyAlarmService.this.stopSelf();
}
};
You are seeing one of the pitfalls when documenting code that would be better off with descriptive variable names. My guess is that they forgot to update their comment when changing the value to 15 seconds.
Personally, I usually introduce a constant variable, e.g.
or alternatively use
and in the code use
or respectively
That way it is both clear what the code does, and it is also clear if I decide to change the values as it is easy to remember to change the variable names.