FileDocCategorySizeDatePackage
VoiceInteractionService.javaAPI DocAndroid 5.1 API9609Thu Mar 12 22:22:10 GMT 2015android.service.voice

VoiceInteractionService

public class VoiceInteractionService extends android.app.Service
Top-level service of the current global voice interactor, which is providing support for hotwording etc. The current VoiceInteractionService that has been selected by the user is kept always running by the system, to allow it to do things like listen for hotwords in the background.

Because this service is always running, it should be kept as lightweight as possible. Heavy-weight operations (including showing UI) should be implemented in the associated {@link android.service.voice.VoiceInteractionSessionService} that only runs while the operation is active.

Fields Summary
public static final String
SERVICE_INTERFACE
The {@link Intent} that must be declared as handled by the service. To be supported, the service must also require the {@link android.Manifest.permission#BIND_VOICE_INTERACTION} permission so that other applications can not abuse it.
public static final String
SERVICE_META_DATA
Name under which a VoiceInteractionService component publishes information about itself. This meta-data should reference an XML resource containing a <{@link android.R.styleable#VoiceInteractionService voice-interaction-service}> tag.
IVoiceInteractionService
mInterface
MyHandler
mHandler
com.android.internal.app.IVoiceInteractionManagerService
mSystemService
private final Object
mLock
private android.hardware.soundtrigger.KeyphraseEnrollmentInfo
mKeyphraseEnrollmentInfo
private AlwaysOnHotwordDetector
mHotwordDetector
static final int
MSG_READY
static final int
MSG_SHUTDOWN
static final int
MSG_SOUND_MODELS_CHANGED
Constructors Summary
Methods Summary
public final AlwaysOnHotwordDetectorcreateAlwaysOnHotwordDetector(java.lang.String keyphrase, java.util.Locale locale, AlwaysOnHotwordDetector.Callback callback)
Creates an {@link AlwaysOnHotwordDetector} for the given keyphrase and locale. This instance must be retained and used by the client. Calling this a second time invalidates the previously created hotword detector which can no longer be used to manage recognition.

param
keyphrase The keyphrase that's being used, for example "Hello Android".
param
locale The locale for which the enrollment needs to be performed.
param
callback The callback to notify of detection events.
return
An always-on hotword detector for the given keyphrase and locale.

        if (mSystemService == null) {
            throw new IllegalStateException("Not available until onReady() is called");
        }
        synchronized (mLock) {
            // Allow only one concurrent recognition via the APIs.
            safelyShutdownHotwordDetector();
            mHotwordDetector = new AlwaysOnHotwordDetector(keyphrase, locale, callback,
                    mKeyphraseEnrollmentInfo, mInterface, mSystemService);
        }
        return mHotwordDetector;
    
protected voiddump(java.io.FileDescriptor fd, java.io.PrintWriter pw, java.lang.String[] args)

        pw.println("VOICE INTERACTION");
        synchronized (mLock) {
            pw.println("  AlwaysOnHotwordDetector");
            if (mHotwordDetector == null) {
                pw.println("    NULL");
            } else {
                mHotwordDetector.dump("    ", pw);
            }
        }
    
protected final android.hardware.soundtrigger.KeyphraseEnrollmentInfogetKeyphraseEnrollmentInfo()

return
Details of keyphrases available for enrollment.
hide

        return mKeyphraseEnrollmentInfo;
    
public static booleanisActiveService(android.content.Context context, android.content.ComponentName service)
Check whether the given service component is the currently active VoiceInteractionService.

        String cur = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.VOICE_INTERACTION_SERVICE);
        if (cur == null || cur.isEmpty()) {
            return false;
        }
        ComponentName curComp = ComponentName.unflattenFromString(cur);
        if (curComp == null) {
            return false;
        }
        return curComp.equals(service);
    
public android.os.IBinderonBind(android.content.Intent intent)

        if (SERVICE_INTERFACE.equals(intent.getAction())) {
            return mInterface.asBinder();
        }
        return null;
    
public voidonCreate()

        super.onCreate();
        mHandler = new MyHandler();
    
public voidonReady()
Called during service initialization to tell you when the system is ready to receive interaction from it. You should generally do initialization here rather than in {@link #onCreate()}. Methods such as {@link #startSession(Bundle)} and {@link #createAlwaysOnHotwordDetector(String, Locale, android.service.voice.AlwaysOnHotwordDetector.Callback)} will not be operational until this point.

        mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
        mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager());
    
public voidonShutdown()
Called during service de-initialization to tell you when the system is shutting the service down. At this point this service may no longer be the active {@link VoiceInteractionService}.

    
private voidonShutdownInternal()

        onShutdown();
        // Stop any active recognitions when shutting down.
        // This ensures that if implementations forget to stop any active recognition,
        // It's still guaranteed to have been stopped.
        // This helps with cases where the voice interaction implementation is changed
        // by the user.
        safelyShutdownHotwordDetector();
    
private voidonSoundModelsChangedInternal()

        synchronized (this) {
            if (mHotwordDetector != null) {
                // TODO: Stop recognition if a sound model that was being recognized gets deleted.
                mHotwordDetector.onSoundModelsChanged();
            }
        }
    
private voidsafelyShutdownHotwordDetector()

        try {
            synchronized (mLock) {
                if (mHotwordDetector != null) {
                    mHotwordDetector.stopRecognition();
                    mHotwordDetector.invalidate();
                    mHotwordDetector = null;
                }
            }
        } catch (Exception ex) {
            // Ignore.
        }
    
public voidstartSession(android.os.Bundle args)
Initiate the execution of a new {@link android.service.voice.VoiceInteractionSession}.

param
args Arbitrary arguments that will be propagated to the session.

        if (mSystemService == null) {
            throw new IllegalStateException("Not available until onReady() is called");
        }
        try {
            mSystemService.startSession(mInterface, args);
        } catch (RemoteException e) {
        }