java - haddler 中的语音识别器似乎每次都重新打开 Activity

标签 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 它会起作用,但是当我在处理程序中收到包含“你好”的消息时,speechRecognizer 启动但很快关闭,每次我发送该消息时都会发生这种情况。

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

任何ideea我怎样才能使这项工作?我想要 toogleButton 中的功能,但也需要处理程序中的功能,因此我可以在处理程序中使用消息或其他内容触发 speechRecognition。

最佳答案

我认为问题在于您正在更改 Handler 中的 toggleButton 状态

toggleButton.setChecked(true);

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

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

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

关于java - haddler 中的语音识别器似乎每次都重新打开 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32392830/

相关文章:

java - 添加soap header 的C#代码 wsse :Security, wsse :BinarySecurityToken, ds :Signature, wsse :UsernameToken, wsu:Timestamp

android - 工具栏标题文字大小

android - Horizo​​ntalScrollView smoothScrolTo 不起作用

java - Activity 崩溃(文件处理和相机)

tomcat - 我想创建处理程序来捕获 Tomcat 上所有 Web 应用程序的日志,并且

java - 检测嵌入 Jetty 时处理程序何时无法启动

java - 从文件中读取并将第一个整数存储为字符串,其余的存储为整数

java - 在Java中访问嵌套类的字段

Java 的 Math.pow() 在简单情况下产生奇怪的结果

javascript - ColdFusion.Window.create 回调处理程序问题