haddler中的Speech Recognizer似乎每次都会再次打开活动

时间:2015-09-04 07:50:51

标签: java android handler speech-recognition

@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,但是当我收到的消息,这些消息,并且#34; ello"在处理程序中,speechRecognizer启动但很快关闭,每当我发送该消息时都会发生这种情况。

此外,它似乎是我在触发带有该消息的处理程序时创建的新活动,但是在toogleButton工作中具有相同的功能。

任何想法我怎样才能做到这一点?我想要toogleButton中的功能,但在处理程序中也是如此,所以我可以使用处理程序内的消息或其他内容触发speechRecognition。

1 个答案:

答案 0 :(得分:2)

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

内的Handler状态
toggleButton.setChecked(true);

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

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

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