0

I am trying to hide the speech recogniser dialog that shows up saying "speak now" and written google as the title ,whenever i call it for passing some voice command. I don't want it to show up. Instead I want it to work in teh background so that the user cannot see it. What should be the correct way to do it?

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

I tried using this but it didn't work:-

getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
lmgguy
  • 89
  • 2
  • 8
  • You need to use `SpeechRecognizer` https://developer.android.com/reference/android/speech/SpeechRecognizer.html but very buggy.. – Sunil Sunny Jun 07 '17 at 09:53
  • isn't there anyother way..I m also using a fullscreen image view so can I put the dialog box that appears at the back of the image view ..? – lmgguy Jun 07 '17 at 09:56
  • No you can't do that. Even if you manage to hide it behind the imageview all the click events will be taken by imageview . This is a good example of how to use SpeechRecognizer https://stackoverflow.com/q/4559930/3111083 . – Sunil Sunny Jun 07 '17 at 10:04
  • Possible duplicate of [How can I use speech recognition without the annoying dialog in android phones](https://stackoverflow.com/questions/6316937/how-can-i-use-speech-recognition-without-the-annoying-dialog-in-android-phones) – Nikolay Shmyrev Jun 18 '17 at 12:48

1 Answers1

1

Try this. This listener catches the events and you can process them accordingly.

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

//Do whatever you want

});  
  • I don't know how to use it ..a documentation will be helpful; isnt there any way I can make my code to work that way ..maybe by hiding it behind the imageview that i have or something? – lmgguy Jun 07 '17 at 09:54
  • Yes you can but hiding it behind something is not a good approach and it may results in slow responsiveness of the application –  Jun 07 '17 at 09:57
  • how would I hide it ?? I also came to know that SpeechRecogniser is very buggy so?? – lmgguy Jun 07 '17 at 10:04
  • You want to try the lazy way in any case? It will slow down the whole application –  Jun 07 '17 at 10:11
  • @Imgguy It's buggy but people are still using it, try this http://www.truiton.com/2014/06/android-speech-recognition-without-dialog-custom-activity/ – Sunil Sunny Jun 07 '17 at 10:11