5

Android 13 removes the SpeechService implementation including RecognitionService, according to this page. It also mentions:

"Apps should use the device's default provider for SpeechService, rather than hard-coding a specific app."

This is the code I am using on APIs less than 13:

String GOOGLE_RECOGNITION_SERVICE_NAME =
        "com.google.android.googlequicksearchbox/com.google.android.voicesearch.serviceapi.GoogleRecognitionService";

SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context,
            ComponentName.unflattenFromString(GOOGLE_RECOGNITION_SERVICE_NAME));

Any ideas on how to get device's default provider for SpeechService?

stavros.3p
  • 2,244
  • 6
  • 20
  • 37

1 Answers1

1

I solved this issue by implementing this code:

public static String getAvailableVoiceRecognitionService(Activity activity)
{
    final List<ResolveInfo> services = activity.getPackageManager().queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);

    String recognitionServiceName = null;

    for (final ResolveInfo info : services)
    {
        String packageName = info.serviceInfo.packageName;
        String serviceName = info.serviceInfo.name;

        String testRecognitionServiceName = packageName + "/" + serviceName;

        ServiceConnection connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };

        Intent serviceIntent = new Intent(RecognitionService.SERVICE_INTERFACE);

        ComponentName recognizerServiceComponent =
                ComponentName.unflattenFromString(testRecognitionServiceName);

        if (recognizerServiceComponent != null)
        {
            serviceIntent.setComponent(recognizerServiceComponent);
            try
            {
                boolean isServiceAvailableToBind = activity.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
                if (isServiceAvailableToBind) {
                    activity.unbindService(connection);
                    recognitionServiceName=testRecognitionServiceName;
                    break;
                }
            }
            catch (SecurityException e)
            {
                e.printStackTrace();
            }
        }
    }

    return recognitionServiceName;
}

Then to initiate the SpeechRecognizer you do:

public void initSpeechRecognition(Activity activity)
{
   String recognitionServiceName = getAvailableVoiceRecognitionService(activity);
    if (recognitionServiceName==null)
        return;

    speechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity,
            ComponentName.unflattenFromString(recognitionServiceName));
 }



    
stavros.3p
  • 2,244
  • 6
  • 20
  • 37
  • 1
    Can you get the languages list by this way in Android 13? I can get the languages list of recognizer "com.google.android.googlequicksearchbox", but it seems the recognizer become "com.google.android.tts" which cannot get language list via RecognizerIntent.ACTION_GET_LANGUAGE_DETAILS. – Fisher Dec 25 '22 at 18:35