【问题标题】:How do I hide the speechRecogniser activity behind the main activity?如何将 SpeechRecognizer 活动隐藏在主要活动后面?
【发布时间】:2017-06-07 09:46:37
【问题描述】:

每当我调用它以传递一些语音命令时,我试图隐藏显示“现在说话”并写 google 作为标题的语音识别器对话框。我不希望它出现。相反,我希望它在后台工作,以便用户看不到它。正确的做法应该是什么?

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

【问题讨论】:

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


【解决方案1】:

试试这个。这个监听器捕捉事件,你可以相应地处理它们。

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

//Do whatever you want

});  

【讨论】:

  • 我不知道如何使用它..文档会有所帮助;有没有什么办法可以让我的代码以这种方式工作..也许通过将它隐藏在我拥有的图像视图或其他东西后面?
  • 是的,您可以,但将其隐藏在某些东西后面并不是一个好方法,它可能会导致应用程序的响应速度变慢
  • 我该如何隐藏它??我也开始知道 SpeechRecogniser 有很多 bug 所以??
  • 无论如何都想试试偷懒的方式?它会减慢整个应用程序的速度
  • @Imgguy 有问题但人们仍在使用它,试试这个truiton.com/2014/06/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多