如何使我的处理程序退出服务中的Runnable?

时间:2018-09-20 22:21:09

标签: java android-studio

我正在运行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());

0 个答案:

没有答案