Creating speech recognition

Hi guys.

I’m trying to create a voice recognition in android. I read some topics about this topic Speech recognition for android mode and then I read about “developers documentation” SpeechRecognizer but it has no sense when I apply my knowledge about Oriented Object Programming to create own speech recognition like google assistant

I started to create the speech recognizer sp object from the class android.speech.SpeechRecognizer and then call the method sp.startListening(intent) but it collapsed

My code is here:

import android.view.View;
import android.content.Context;

import android.view.Gravity;
import android.speech.RecognizerIntent;

import android.content.Intent;
import android.os.Vibrator;///let us take problems one after the other!!!!!
import android.os.VibrationEffect;
import android.service.voice.AlwaysOnHotwordDetector;
import android.speech.SpeechRecognizer;

import android.widget.Toast;
import java.lang.Throwable;
import java.lang.Exception;
import java.lang.RuntimeException;
import android.content.ActivityNotFoundException;
import java.util.Locale;
import android.view.ViewGroup.LayoutParams;
import android.content.Context;



Activity act;
Intent intent;

Context context;
SpeechRecognizer sp;

public void onStart() 
  act = this.getActivity();
  context = act.getApplicationContext();

void setup() 
  act = this.getActivity();
  //SpeechRecognizer sp = SpeechRecognizer.createSpeechRecognizer(getActivity());
  sp = SpeechRecognizer.createSpeechRecognizer(getActivity());

void draw() {

I realized that with some hotwords the libraries could activate the speech, but if I don´t create the object speech recognizer, i could not follow the next step…

could you please help me?

@juanjoc ===
the code you have put cannot work for a lot of reasons:

  • Main one is that you have to create the speechRecognizer on the main ui and using context as parameter
  • Second one is that your intent is undefined:see my post from your link
  • Then there are two ways as i have explained in the post (from you link); either you use startActivityforResult (i have put code for that in the same link) and you get the google dialog, or you create your own recognitionListener (implementing the required methods when you create it) and you can see the result without the init google logo;but it is far more complicated
  • Be sure to ask for permissions on runtime (record_audio)

How do I do this?



@Chrisir ===
like that:

import android.Manifest;
import android.os.Build;
int MyVersion = Build.VERSION.SDK_INT;
Activity act;

void setup(){
  act = this.getActivity();
  if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
                if (!permissionsDejaAccordees()) {


void draw(){

private boolean permissionsDejaAccordees() {
    int result = act.checkSelfPermission( Manifest.permission.RECORD_AUDIO);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;

private void demandePermissionParticuliere()  {
        act.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 101);

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 101:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               println("permissions accordées");
            } else {
                println("permissions not granted");
            act.onRequestPermissionsResult(requestCode, permissions, grantResults);

ok, what is the difference between making a permissions code on runtime and only at processing permissions tab

those are the same?

@juanjoc === no, permissions asked through Manifest do not work after lollipop (see my code); now permissions can only be given by the user on runtime.