1

I using SpeechRecognizer in Service, it work only first time when I call speechRecognizer.startListening(), and then it has an error with code 9 (insufficient permission).

If in Activity, it work well.

Permissions in my app:

<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Recognizer Intent

private val intent by lazy {
        Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).run {
            putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
            putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
            putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.applicationContext.packageName)
        }
    }

Please help me.

Thanks in advance.

PhongBM
  • 799
  • 10
  • 23

3 Answers3

0

You can use SpeechRecognizer instead RecognizerIntent. It works in service well. You init it in OnStartCommand like this:

    @Override
public int onStartCommand(Intent intent,int flags,int startId) {

    Toast.makeText(this,"start Service.",Toast.LENGTH_SHORT).show();

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(getApplicationContext());
    speechRecognizer.setRecognitionListener(this);

    Intent voice = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    voice.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass()
            .getPackage().getName());
    voice.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    voice.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);

    speechRecognizer.startListening(voice);

    return START_REDELIVER_INTENT;
}

For more detail : https://developer.android.com/reference/android/speech/SpeechRecognizer

Tungken
  • 1,917
  • 1
  • 15
  • 18
0

I'm quite new to this Service topic but does the official documentation of SpeechRecognizer point out that you can't use it when you're not on the MainThread?

This class's methods must be invoked only from the main application thread.

https://developer.android.com/reference/android/speech/SpeechRecognizer

And this might be stupid but did you ask the user for those permissions at runtime?

I should have put this as a comment instead of an answer but the editor of Answer works way better.

Harvey
  • 1,353
  • 1
  • 14
  • 27
0

Using createOnDeviceSpeechRecognizer instead of createSpeechRecognizer worked for me in Android 13 (Samsung)

I had provided the microphone permissions for both my and Google app and calling from the main thread but still wasn't working.

https://developer.android.com/reference/android/speech/SpeechRecognizer

UzumakiL
  • 373
  • 3
  • 9