Added in API level 8

RecognitionService


abstract class RecognitionService : Service
kotlin.Any
   ↳ android.content.Context
   ↳ android.content.ContextWrapper
   ↳ android.app.Service
   ↳ android.speech.RecognitionService

This class provides a base class for recognition service implementations. This class should be extended only in case you wish to implement a new speech recognizer. Please note that the implementation of this service is stateless.

Summary

Nested classes
open

This class receives callbacks from the speech recognition service and forwards them to the user.

open

This class receives callbacks from the speech recognition service and forwards them to the user.

Constants
static String

The Intent that must be declared as handled by the service.

static String

Name under which a RecognitionService component publishes information about itself.

Inherited constants
Public constructors

Public methods
open Context
createContext(contextParams: ContextParams)

Creates a context with specific properties and behaviors.

open Int

Returns the maximal number of recognition sessions ongoing at the same time.

IBinder?
onBind(intent: Intent!)

open Unit

Queries the service on whether it would support a onStartListening(android.content.Intent,android.speech.RecognitionService.Callback) for the same recognizerIntent.

open Unit
onCheckRecognitionSupport(recognizerIntent: Intent, attributionSource: AttributionSource, supportCallback: RecognitionService.SupportCallback)

Queries the service on whether it would support a onStartListening(android.content.Intent,android.speech.RecognitionService.Callback) for the same recognizerIntent.

open Unit

open Unit
onTriggerModelDownload(recognizerIntent: Intent)

Requests the download of the recognizer support for recognizerIntent.

open Unit
onTriggerModelDownload(recognizerIntent: Intent, attributionSource: AttributionSource)

Requests the download of the recognizer support for recognizerIntent.

open Unit
onTriggerModelDownload(recognizerIntent: Intent, attributionSource: AttributionSource, listener: ModelDownloadListener)

Requests the download of the recognizer support for recognizerIntent.

Protected methods
abstract Unit

Notifies the service that it should cancel the speech recognition.

abstract Unit
onStartListening(recognizerIntent: Intent!, listener: RecognitionService.Callback!)

Notifies the service that it should start listening for speech.

abstract Unit

Notifies the service that it should stop listening for speech.

Inherited functions

Constants

SERVICE_INTERFACE

Added in API level 8
static val SERVICE_INTERFACE: String

The Intent that must be declared as handled by the service.

Value: "android.speech.RecognitionService"

SERVICE_META_DATA

Added in API level 8
static val SERVICE_META_DATA: String

Name under which a RecognitionService component publishes information about itself. This meta-data should reference an XML resource containing a <recognition-service> or <on-device-recognition-service > tag.

Value: "android.speech"

Public constructors

RecognitionService

RecognitionService()

Public methods

createContext

Added in API level 31
open fun createContext(contextParams: ContextParams): Context

Creates a context with specific properties and behaviors.

Parameters
contextParams ContextParams: This value cannot be null.
Return
Context This value cannot be null.

getMaxConcurrentSessionsCount

Added in API level 34
open fun getMaxConcurrentSessionsCount(): Int

Returns the maximal number of recognition sessions ongoing at the same time.

The default value is 1, meaning concurrency should be enabled by overriding this method.

onBind

Added in API level 8
fun onBind(intent: Intent!): IBinder?
Parameters
intent Intent!: The Intent that was used to bind to this service, as given to android.content.Context#bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Return
IBinder? Return an IBinder through which clients can call on to the service.

onCheckRecognitionSupport

Added in API level 33
open fun onCheckRecognitionSupport(
    recognizerIntent: Intent,
    supportCallback: RecognitionService.SupportCallback
): Unit

Queries the service on whether it would support a onStartListening(android.content.Intent,android.speech.RecognitionService.Callback) for the same recognizerIntent.

The service will notify the caller about the level of support or error via SupportCallback.

If the service does not offer the support check it will notify the caller with SpeechRecognizer#ERROR_CANNOT_CHECK_SUPPORT.

Parameters
recognizerIntent Intent: This value cannot be null.
supportCallback RecognitionService.SupportCallback: This value cannot be null.

onCheckRecognitionSupport

Added in API level 34
open fun onCheckRecognitionSupport(
    recognizerIntent: Intent,
    attributionSource: AttributionSource,
    supportCallback: RecognitionService.SupportCallback
): Unit

Queries the service on whether it would support a onStartListening(android.content.Intent,android.speech.RecognitionService.Callback) for the same recognizerIntent.

The service will notify the caller about the level of support or error via SupportCallback.

If the service does not offer the support check it will notify the caller with SpeechRecognizer#ERROR_CANNOT_CHECK_SUPPORT.

Provides the calling AttributionSource to the service implementation so that permissions and bandwidth could be correctly blamed.

Parameters
recognizerIntent Intent: This value cannot be null.
attributionSource AttributionSource: This value cannot be null.
supportCallback RecognitionService.SupportCallback: This value cannot be null.

onDestroy

Added in API level 8
open fun onDestroy(): Unit

onTriggerModelDownload

Added in API level 33
open fun onTriggerModelDownload(recognizerIntent: Intent): Unit

Requests the download of the recognizer support for recognizerIntent.

Parameters
recognizerIntent Intent: This value cannot be null.

onTriggerModelDownload

Added in API level 34
open fun onTriggerModelDownload(
    recognizerIntent: Intent,
    attributionSource: AttributionSource
): Unit

Requests the download of the recognizer support for recognizerIntent.

Provides the calling AttributionSource to the service implementation so that permissions and bandwidth could be correctly blamed.

Parameters
recognizerIntent Intent: This value cannot be null.
attributionSource AttributionSource: This value cannot be null.

onTriggerModelDownload

Added in API level 34
open fun onTriggerModelDownload(
    recognizerIntent: Intent,
    attributionSource: AttributionSource,
    listener: ModelDownloadListener
): Unit

Requests the download of the recognizer support for recognizerIntent.

Provides the calling AttributionSource to the service implementation so that permissions and bandwidth could be correctly blamed.

Client will receive the progress updates via the given ModelDownloadListener:

  • If the model is already available, ModelDownloadListener#onSuccess() will be called directly. The model can be safely used afterwards.
  • If the RecognitionService has started the download, ModelDownloadListener#onProgress(int) will be called an unspecified (zero or more) number of times until the download is complete. When the download finishes, ModelDownloadListener#onSuccess() will be called. The model can be safely used afterwards.
  • If the RecognitionService has only scheduled the download, but won't satisfy it immediately, ModelDownloadListener#onScheduled() will be called. There will be no further updates on this listener.
  • If the request fails at any time due to a network or scheduling error, ModelDownloadListener#onError(int) will be called.
  • Parameters
    recognizerIntent Intent: contains parameters for the recognition to be performed. The intent may also contain optional extras, see RecognizerIntent. This value cannot be null.
    attributionSource AttributionSource: the attribution source of the caller. This value cannot be null.
    listener ModelDownloadListener: on which to receive updates about the model download request. This value cannot be null.

    Protected methods

    onCancel

    Added in API level 8
    protected abstract fun onCancel(listener: RecognitionService.Callback!): Unit

    Notifies the service that it should cancel the speech recognition.

    onStartListening

    Added in API level 8
    protected abstract fun onStartListening(
        recognizerIntent: Intent!,
        listener: RecognitionService.Callback!
    ): Unit

    Notifies the service that it should start listening for speech.

    If you are recognizing speech from the microphone, in this callback you should create an attribution context for the caller such that when you access the mic the caller would be properly blamed (and their permission checked in the process) for accessing the microphone and that you served as a proxy for this sensitive data (and your permissions would be checked in the process). You should also open the mic in this callback via the attribution context and close the mic before returning the recognized result. If you don't do that then the caller would be blamed and you as being a proxy as well as you would get one more blame on yourself when you open the microphone.

    Context attributionContext = context.createContext(new ContextParams.Builder()
          .setNextAttributionSource(callback.getCallingAttributionSource())
          .build());
     
      AudioRecord recorder = AudioRecord.Builder()
          .setContext(attributionContext);
          . . .
         .build();
     
      recorder.startRecording()
      
    Parameters
    recognizerIntent Intent!: contains parameters for the recognition to be performed. The intent may also contain optional extras, see RecognizerIntent. If these values are not set explicitly, default values should be used by the recognizer.
    listener RecognitionService.Callback!: that will receive the service's callbacks

    onStopListening

    Added in API level 8
    protected abstract fun onStopListening(listener: RecognitionService.Callback!): Unit

    Notifies the service that it should stop listening for speech. Speech captured so far should be recognized as if the user had stopped speaking at this point. This method is only called if the application calls it explicitly.