haddler 中的语音识别器似乎每次都再次打开 activity

Speech Recognizer in haddler seems to open the activity again every time

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    returnedText = (TextView) findViewById(R.id.textView1);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);

    progressBar.setVisibility(View.INVISIBLE);
    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
            "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);


        toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {


        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            if (isChecked) {
                progressBar.setVisibility(View.VISIBLE);
                progressBar.setIndeterminate(true);
                speech.startListening(recognizerIntent);
            } else {
                progressBar.setIndeterminate(false);
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
            }
        }
    });
    ttsManager = new TTSManager();
    ttsManager.init(this);

    Pause = (Button) findViewById(R.id.pause);
    Stop = (Button) findViewById(R.id.stop);
    Resume = (Button) findViewById(R.id.resume);
    Pause.setVisibility(View.INVISIBLE);
    Resume.setVisibility(View.INVISIBLE);
    Stop.setVisibility(View.INVISIBLE);


    // -------------------------------------


    //----------------------------------------


    handler = new android.os.Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case RECIEVE_MESSAGE:                                                   // if receive massage
                    byte[] readBuf = (byte[]) msg.obj;
                    String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                    sb.append(strIncom);                                                // append string
                    int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                    if (endOfLineIndex > 0) {                                           // if end-of-line,
                        String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                        sb.delete(0, sb.length());                                      // and clear
                        Log.e("TAG", sbprint);

                        if(sbprint.contains("ello")){
                            progressBar.setVisibility(View.VISIBLE);
                            progressBar.setIndeterminate(true);
                            speech.startListening(recognizerIntent);
                            toggleButton.setChecked(true);

                        }




                    }
                    //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                    break;
            }
        };
    };


    btAdapter = BluetoothAdapter.getDefaultAdapter();       // get Bluetooth adapter
    checkBTState();

    //--------------------------------------


}

我正在尝试激活语音,就像按下 toogleButton 一样,我将代码从 toogleButton 复制到处理程序。 一切正常,我的意思是,如果我单击 toogleButton 它会起作用,但是当我在处理程序中收到包含 "ello" 的消息时,speechRecognizer 启动但很快关闭,每次我发送该消息时都会发生这种情况。

另外,这似乎是我在使用该消息触发处理程序时创建的新 activity,但 toogleButton 中的相同功能有效。

知道我怎样才能完成这项工作吗?我想要 toogleButton 中的功能,但也需要处理程序中的功能,因此我可以使用消息或处理程序内部的内容触发 speechRecognition。

我认为问题在于您正在更改 Handler

toggleButton 的状态
toggleButton.setChecked(true);

这将触发 OnCheckedChangeListener。但这应该会再次启动语音识别(因为您将其设置为 true),而不是停止它。

尝试在您的 Handler 中仅调用 toggleButton.setChecked(true);,看看是否可以解决问题,例如:

if(sbprint.contains("ello")){
     toggleButton.setChecked(true);
}