如何隐藏主要活动背后的speechRecogniser活动?

时间:2017-06-07 09:46:38

标签: android android-activity speech-recognition google-speech-api

我正在尝试隐藏语音识别器对话框,该对话框显示说“现在说话”并将谷歌写成标题,每当我呼叫它传递一些语音命令时。我不希望它出现。相反,我希望它在背景中工作,以便用户无法看到它。应该采用哪种正确方法?

public void getSpeechInput(View view) {

    getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, 10);
    } else {
        Toast.makeText(this, "Your Device Don't Support Speech Input", Toast.LENGTH_SHORT).show();
    }
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case 10:
            if (resultCode == RESULT_OK && data != null) {
                ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                txvResult.setText(result.get(0));
                String str = txvResult.getText().toString();


                if(str.equals("lights on")){
                    Toast.makeText(MainActivity.this,
                            "matched", Toast.LENGTH_LONG).show();
                    count[0]=1;
                    turnOnFlash();
                }
                else if(str.equals("lights off")){
                    Toast.makeText(MainActivity.this,
                           "didnt", Toast.LENGTH_LONG).show();
                    count[0]=0;
                    turnOffFlash();
                }
            }
            break;
    }
}

我尝试过使用它,但它不起作用: -

getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);

1 个答案:

答案 0 :(得分:1)

试试这个。此侦听器会捕获事件,您可以相应地处理它们。

SpeechRecognizer recognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognizer.setRecognitionListener(new RecognitionListener() 
{

//Do whatever you want

});