Android boolean is not keeping its state

65 Views Asked by At

So, I have a boolean called nuanceWaiting it's initially set to true. I immediate run a runnable loop that checks if nuanceWaiting is true or false.

protected void onCreate(Bundle savedInstanceState) {
    ...
    nuanceWaiting = true;
    ...
}
@Override
protected void onStart() {
    ....
    soundMeterLoop();
}
public void soundMeterLoop() {
    soundMeterHandler = new Handler();
    soundMeterRunnable = new Runnable() {
        @Override
        public void run() {
            if(nuanceWaiting) {
                //do my stuff
                amplitude = soundMeter.getAmplitude();
                if (amplitude > threshold) {
                    decibelLevelOutput.setTextColor(Color.RED);
                    startNuance();
                } else {
                    decibelLevelOutput.setTextColor(Color.BLACK);
                }
            }
            soundMeterHandler.postDelayed(this, 100);
        }
    };
    soundMeterHandler.postDelayed(soundMeterRunnable, 100);
}
public void startNuance() {
    nuanceWaiting = false;
    nuance.toggleReco();
}
public void stopNuance() {
    Log.d("SpeechKit", "stopNuance");
    nuanceWaiting = true;
    Log.d("SpeechKit", "nuanceWaiting " + nuanceWaiting);
}

Now, for some reason, once I call false, now, nuance.toggleReco() goes to another class and when it's finished, it calls stopNuance();

nuanceWaiting becomes false (showing in the second log), but when I check a log in the runnable, it still says true and never "stays" false when running the runnable again. Any idea as to why it doesn't stick to being false?

Below is what nuance.toggleReco(); does

public void toggleReco() {
    Log.d("SpeechKit", "In "+state);
    switch (state) {
        case IDLE:
            recognize();
            break;
        case LISTENING:
            stopRecording();
            break;
        case PROCESSING:
            cancel();
            break;
    }
}

It's usually in the IDLE state, so I'll follow that method,

private void recognize() {
    //Setup our ASR transaction options.
    Transaction.Options options = new Transaction.Options();
    options.setRecognitionType(RecognitionType.DICTATION);
    options.setDetection(DetectionType.Short);
    options.setLanguage(new Language("eng-USA"));
    options.setEarcons(startEarcon, stopEarcon, errorEarcon, cancelEarcon);

    //Start listening
    recoTransaction = session.recognize(options, recoListener);
}

private Transaction.Listener recoListener = new Transaction.Listener() {
    @Override
    public void onStartedRecording(Transaction transaction) {
        Log.d("SpeechKit", "onStartedRecording");
        //We have started recording the users voice.
        //We should update our state and start polling their volume.
        state = State.LISTENING;
        startAudioLevelPoll();
    }

    @Override
    public void onFinishedRecording(Transaction transaction) {
        Log.d("SpeechKit", "onFinishedRecording");
        //We have finished recording the users voice.
        //We should update our state and stop polling their volume.
        state = State.PROCESSING;
        stopAudioLevelPoll();
        avatar.stopNuance();
    }

    @Override
    public void onRecognition(Transaction transaction, Recognition recognition) {
        //We have received a transcription of the users voice from the server.
        state = State.IDLE;
        Log.d("SpeechKit", "onRecognition: " + recognition.getText());
        voiceRecognizeText = recognition.getText();
        voiceRecognize = (TextView) activity.findViewById(R.id.voiceRecognize);
        voiceRecognize.setText(voiceRecognizeText);
    }

    @Override
    public void onSuccess(Transaction transaction, String s) {
        Log.d("SpeechKit", "onSuccess");
        //Notification of a successful transaction. Nothing to do here.
    }

    @Override
    public void onError(Transaction transaction, String s, TransactionException e) {
        Log.e("SpeechKit", "onError: " + e.getMessage() + ". " + s);
        //Something went wrong. Ensure that your credentials are correct.
        //The user could also be offline, so be sure to handle this case appropriately.
        //We will simply reset to the idle state.
        state = State.IDLE;
        avatar.stopNuance();
    }
};
0

There are 0 best solutions below