Android布尔值没有保持其状态

时间:2016-12-30 20:40:38

标签: android runnable

所以,我有一个名为nuanceWaiting的布尔值,它最初设置为true。我立即运行一个runnable循环来检查nuanceWaiting是真还是假。

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);
}

现在,出于某种原因,一旦我调用false,现在,nuance.toggleReco()会转到另一个类,当它完成时,它会调用stopNuance();

nuanceWaiting变为false(显示在第二个日志中),但是当我检查runnable中的日志时,它仍然显示为true,并且在再次运行runnable时从不“保持”错误。知道为什么它不坚持虚假吗?

以下是nuance.toggleReco();的作用

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

它通常处于IDLE状态,因此我将遵循该方法,

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 个答案:

没有答案