How to open an event using a voice command

I am using a toolbar application that has many screens. When the user says a voice command based on this, I need to open the action. I don't know where to start. I have finished all the screens already and I would like to implement voice searches. my app screens are Advances, Leaves, Recruitment, Permissions, Notifications, etc. Example: when the user says "Achievement", he should open the progress screens. Please help me.

+3


source to share


1 answer


1) Start intent voice recognition

2) Handle the returned data in onActivityResult () to decide which activity to run

1. Launch intent voice recognition



Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Choose Activity");
startActivityForResult(intent, REQUEST_SPEECH);

      

2. Process the returned data

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode == REQUEST_SPEECH){
            if (resultCode == RESULT_OK){
                ArrayList<String> matches = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

                if (matches.size() == 0) {
                    // didn't hear anything
                } else {
                    String mostLikelyThingHeard = matches.get(0);
                    // toUpperCase() used to make string comparison equal
                    if(mostLikelyThingHeard.toUpperCase().equals("ADVANCES")){
                        startActivity(new Intent(this, Advances.class));
                    } else if() {
                    }
                }
            }
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

      

+3


source







All Articles