我正在运行Service
,它会打开Handler
,并使其连续运行直到事件发生。然后,一旦isSafe
变量为true
,我希望处理程序与Service
一起关闭。但是,Handler
仍在继续并且不会停止。我可以告诉您,因为语音监听器会一直关闭并且不会退出。如何退出Runnable
?
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
if (prefVoice.getBoolean("WordMatch", false) == false) {
VoiceLoop();
handler.postDelayed(this, postChanger());
} else if (prefVoice.getBoolean("WordMatch", false) == true) {
if (prefVoice.getBoolean("isSafe", false) == true) {
prefVoice.edit().putBoolean("WordMatch", false).apply();
handler.removeCallbacksAndMessages(this);
stopSelf();
} else{
if (prefVoice.getBoolean("isSafe", false)) {
} else{
String GPS = getGPS();
phoneFunctions.sendTextMessage(numbers, message, GPS);
handler.postDelayed(this, postChanger());
}
}
}
}
};
handler.postDelayed(runnable, postChanger());