Java haddler中的语音识别器似乎每次都会再次打开活动

Java haddler中的语音识别器似乎每次都会再次打开活动,java,android,handler,speech-recognition,Java,Android,Handler,Speech Recognition,我试图激活语音,就像我按下toogleButton一样,我将代码从toogleButton复制到处理程序。 一切正常,我的意思是,如果我单击toogleButton,它会工作,但当我收到处理程序中包含“ello”的消息时,SpeechRecognitor会启动,但会很快关闭,每次我发送该消息时都会发生这种情况 另外,当我使用该消息触发处理程序时,它看起来像是我创建的一个新活动,但toogleButton中的相同函数也起作用 有什么想法吗?我怎样才能做到这一点?我想要toogleButton中的功

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

另外,当我使用该消息触发处理程序时,它看起来像是我创建的一个新活动,但toogleButton中的相同函数也起作用


有什么想法吗?我怎样才能做到这一点?我想要toogleButton中的功能,但处理程序中也需要功能,因此我可以通过处理程序中的消息或内容触发speechRecognition。

我认为问题在于您正在更改
处理程序中的
切换按钮的状态

@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();

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


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

尝试仅调用
toggleButton.setChecked(true)
处理程序中,查看这是否解决了问题,例如:

toggleButton.setChecked(true);

我将检查这是5分钟:它工作,但仍然打开一个新的活动或其他东西,因为如果我按下后退某个东西关闭..在处理程序中,它开始一个新的活动,并且使用tooglebutton在处理程序中,你只能调用
toggleButton.setChecked(true)
这将触发OnCheckedChangeListener的
,并启动语音识别。@LaurentiuSimionescu Cu placere si在continuare中成功。
if(sbprint.contains("ello")){
     toggleButton.setChecked(true);
}