Merge "Fixed cancel() not working correctly"
This commit is contained in:
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package android.os;
|
package android.os;
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule a countdown until a time in the future, with
|
* Schedule a countdown until a time in the future, with
|
||||||
* regular notifications on intervals along the way.
|
* regular notifications on intervals along the way.
|
||||||
@@ -57,6 +55,11 @@ public abstract class CountDownTimer {
|
|||||||
|
|
||||||
private long mStopTimeInFuture;
|
private long mStopTimeInFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* boolean representing if the timer was cancelled
|
||||||
|
*/
|
||||||
|
private boolean mCancelled = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param millisInFuture The number of millis in the future from the call
|
* @param millisInFuture The number of millis in the future from the call
|
||||||
* to {@link #start()} until the countdown is done and {@link #onFinish()}
|
* to {@link #start()} until the countdown is done and {@link #onFinish()}
|
||||||
@@ -72,7 +75,8 @@ public abstract class CountDownTimer {
|
|||||||
/**
|
/**
|
||||||
* Cancel the countdown.
|
* Cancel the countdown.
|
||||||
*/
|
*/
|
||||||
public final void cancel() {
|
public synchronized final void cancel() {
|
||||||
|
mCancelled = true;
|
||||||
mHandler.removeMessages(MSG);
|
mHandler.removeMessages(MSG);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +84,7 @@ public abstract class CountDownTimer {
|
|||||||
* Start the countdown.
|
* Start the countdown.
|
||||||
*/
|
*/
|
||||||
public synchronized final CountDownTimer start() {
|
public synchronized final CountDownTimer start() {
|
||||||
|
mCancelled = false;
|
||||||
if (mMillisInFuture <= 0) {
|
if (mMillisInFuture <= 0) {
|
||||||
onFinish();
|
onFinish();
|
||||||
return this;
|
return this;
|
||||||
@@ -112,6 +117,10 @@ public abstract class CountDownTimer {
|
|||||||
public void handleMessage(Message msg) {
|
public void handleMessage(Message msg) {
|
||||||
|
|
||||||
synchronized (CountDownTimer.this) {
|
synchronized (CountDownTimer.this) {
|
||||||
|
if (mCancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
|
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
|
||||||
|
|
||||||
if (millisLeft <= 0) {
|
if (millisLeft <= 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user