如何从处理程序中删除未分配给变量的 Runnable
How to remove a Runnable that is not assigned to a variable from a Handler
我启动了一个runnnable,但没有将它分配给一个变量。我该如何阻止它?我知道它是 removeCallbacksAndMessages() 方法的组合,但我不知道要传递什么参数给这个方法,因为我创建了一个匿名的 runnable:
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
h = new Handler();
final int delay = 1000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int mNumberOfPollsForCurrentDay = (int) dataSnapshot.getChildrenCount();
Random r = new Random();
int randomPollInRangeOfCurrentDayNumberOfPolls = r.nextInt((mNumberOfPollsForCurrentDay + 1) - 1) + 1;
int numberOfPollAnswersAtRandomNumber = (int) dataSnapshot.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).getChildrenCount();
Random rr = new Random();
int randomAnswerBasedFromRandomPollAnswerChoices = rr.nextInt((numberOfPollAnswersAtRandomNumber + 1) - 1) + 1;
mUpdateRef.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).child(String.valueOf(randomAnswerBasedFromRandomPollAnswerChoices)).child("Vote_Count").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
mutableData.setValue((Long) mutableData.getValue() + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
h.postDelayed(this, delay);
}
}, delay);
}
});
调用 h.removeCallbacksAndMessages(null)
将删除 documentation:
中所述的所有回调和消息
Remove any pending posts of callbacks and sent messages whose obj is
token. If token is null, all callbacks and messages will be removed.
请注意,如果 runnable 已经在执行中,这不会停止它,但如果它仍在等待,则会将其删除。
我启动了一个runnnable,但没有将它分配给一个变量。我该如何阻止它?我知道它是 removeCallbacksAndMessages() 方法的组合,但我不知道要传递什么参数给这个方法,因为我创建了一个匿名的 runnable:
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
h = new Handler();
final int delay = 1000; //milliseconds
h.postDelayed(new Runnable() {
public void run() {
mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int mNumberOfPollsForCurrentDay = (int) dataSnapshot.getChildrenCount();
Random r = new Random();
int randomPollInRangeOfCurrentDayNumberOfPolls = r.nextInt((mNumberOfPollsForCurrentDay + 1) - 1) + 1;
int numberOfPollAnswersAtRandomNumber = (int) dataSnapshot.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).getChildrenCount();
Random rr = new Random();
int randomAnswerBasedFromRandomPollAnswerChoices = rr.nextInt((numberOfPollAnswersAtRandomNumber + 1) - 1) + 1;
mUpdateRef.child(String.valueOf(randomPollInRangeOfCurrentDayNumberOfPolls)).child(POLL_ANSWERS_LABEL).child(String.valueOf(randomAnswerBasedFromRandomPollAnswerChoices)).child("Vote_Count").runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
mutableData.setValue((Long) mutableData.getValue() + 1);
return Transaction.success(mutableData);
}
@Override
public void onComplete(FirebaseError firebaseError, boolean b, DataSnapshot dataSnapshot) {
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
h.postDelayed(this, delay);
}
}, delay);
}
});
调用 h.removeCallbacksAndMessages(null)
将删除 documentation:
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.
请注意,如果 runnable 已经在执行中,这不会停止它,但如果它仍在等待,则会将其删除。