FileDocCategorySizeDatePackage
RecognitionService.javaAPI DocAndroid 5.1 API14721Thu Mar 12 22:22:10 GMT 2015android.speech

RecognitionService

public abstract class RecognitionService extends android.app.Service
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.

Fields Summary
public static final String
SERVICE_INTERFACE
The {@link Intent} that must be declared as handled by the service.
public static final String
SERVICE_META_DATA
Name under which a RecognitionService component publishes information about itself. This meta-data should reference an XML resource containing a <{@link android.R.styleable#RecognitionService recognition-service}> tag.
private static final String
TAG
Log messages identifier
private static final boolean
DBG
Debugging flag
private RecognitionServiceBinder
mBinder
Binder of the recognition service
private Callback
mCurrentCallback
The current callback of an application that invoked the {@link RecognitionService#onStartListening(Intent, Callback)} method
private static final int
MSG_START_LISTENING
private static final int
MSG_STOP_LISTENING
private static final int
MSG_CANCEL
private static final int
MSG_RESET
private final android.os.Handler
mHandler
Constructors Summary
Methods Summary
private booleancheckPermissions(IRecognitionListener listener)
Checks whether the caller has sufficient permissions

param
listener to send the error message to in case of error
return
{@code true} if the caller has enough permissions, {@code false} otherwise

        if (DBG) Log.d(TAG, "checkPermissions");
        if (RecognitionService.this.checkCallingOrSelfPermission(android.Manifest.permission.
                RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        try {
            Log.e(TAG, "call for recognition service without RECORD_AUDIO permissions");
            listener.onError(SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS);
        } catch (RemoteException re) {
            Log.e(TAG, "sending ERROR_INSUFFICIENT_PERMISSIONS message failed", re);
        }
        return false;
    
private voiddispatchCancel(IRecognitionListener listener)

        if (mCurrentCallback == null) {
            if (DBG) Log.d(TAG, "cancel called with no preceding startListening - ignoring");
        } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) {
            Log.w(TAG, "cancel called by client who did not call startListening - ignoring");
        } else { // the correct state
            RecognitionService.this.onCancel(mCurrentCallback);
            mCurrentCallback = null;
            if (DBG) Log.d(TAG, "canceling - setting mCurrentCallback to null");
        }
    
private voiddispatchClearCallback()

        mCurrentCallback = null;
    
private voiddispatchStartListening(android.content.Intent intent, IRecognitionListener listener)


           
        if (mCurrentCallback == null) {
            if (DBG) Log.d(TAG, "created new mCurrentCallback, listener = " + listener.asBinder());
            try {
                listener.asBinder().linkToDeath(new IBinder.DeathRecipient() {
                    @Override
                    public void binderDied() {
                        mHandler.sendMessage(mHandler.obtainMessage(MSG_CANCEL, listener));
                    }
                }, 0);
            } catch (RemoteException re) {
                Log.e(TAG, "dead listener on startListening");
                return;
            }
            mCurrentCallback = new Callback(listener);
            RecognitionService.this.onStartListening(intent, mCurrentCallback);
        } else {
            try {
                listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY);
            } catch (RemoteException e) {
                Log.d(TAG, "onError call from startListening failed");
            }
            Log.i(TAG, "concurrent startListening received - ignoring this call");
        }
    
private voiddispatchStopListening(IRecognitionListener listener)

        try {
            if (mCurrentCallback == null) {
                listener.onError(SpeechRecognizer.ERROR_CLIENT);
                Log.w(TAG, "stopListening called with no preceding startListening - ignoring");
            } else if (mCurrentCallback.mListener.asBinder() != listener.asBinder()) {
                listener.onError(SpeechRecognizer.ERROR_RECOGNIZER_BUSY);
                Log.w(TAG, "stopListening called by other caller than startListening - ignoring");
            } else { // the correct state
                RecognitionService.this.onStopListening(mCurrentCallback);
            }
        } catch (RemoteException e) { // occurs if onError fails
            Log.d(TAG, "onError call from stopListening failed");
        }
    
public final android.os.IBinderonBind(android.content.Intent intent)

        if (DBG) Log.d(TAG, "onBind, intent=" + intent);
        return mBinder;
    
protected abstract voidonCancel(android.speech.RecognitionService$Callback listener)
Notifies the service that it should cancel the speech recognition.

public voidonDestroy()

        if (DBG) Log.d(TAG, "onDestroy");
        mCurrentCallback = null;
        mBinder.clearReference();
        super.onDestroy();
    
protected abstract voidonStartListening(android.content.Intent recognizerIntent, android.speech.RecognitionService$Callback listener)
Notifies the service that it should start listening for speech.

param
recognizerIntent contains parameters for the recognition to be performed. The intent may also contain optional extras, see {@link RecognizerIntent}. If these values are not set explicitly, default values should be used by the recognizer.
param
listener that will receive the service's callbacks

protected abstract voidonStopListening(android.speech.RecognitionService$Callback listener)
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.