title: How to make hotword detection service in android
body: <p>I want to create a service that should listens for hotword in background such that when i say hello it should invoke an activity, how can i do this, about voiceInteractionService but I have read that Its not available to use, is it true? could anyone tell me the way i should solve this problem? Its about hotword detector</p>
  
  <p>I have been following <a href="http://androidxref.com/5.0.0_r2/xref/frameworks/base/tests/VoiceInteraction/src/com/android/test/voiceinteraction/" rel="noreferrer">this</a></p>
  
  <p>Tried this:</p>
  
  <pre><code>public class InteractionService extends VoiceInteractionService {
  
  static final String TAG = "InteractionService" ;
  private AlwaysOnHotwordDetector mHotwordDetector;
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      Log.i(TAG, "service started");
  }
  
  @Override
  public void onReady() {
      super.onReady();
      Log.i(TAG, "Creating " + this);
  
      mHotwordDetector = createAlwaysOnHotwordDetector("Hello"
  ,  Locale.forLanguageTag("en-US"), mHotwordCallback);
      Log.i(TAG, "onReady");
  }
  
  private final AlwaysOnHotwordDetector.Callback mHotwordCallback = 
  new AlwaysOnHotwordDetector.Callback() {
             @Override
             public void onAvailabilityChanged(int status) {
                      Log.i(TAG, "onAvailabilityChanged(" + status + ")");
                      hotwordAvailabilityChangeHelper(status);
                  }
  
                      @Override
              public void onDetected(AlwaysOnHotwordDetector
                 .EventPayload eventPayload) {
                      Log.i(TAG, "onDetected");
                  }
  
                      @Override
              public void onError() {
                      Log.i(TAG, "onError");
                  }
  
                      @Override
              public void onRecognitionPaused() {
                      Log.i(TAG, "onRecognitionPaused");
                  }
  
                      @Override
              public void onRecognitionResumed() {
                      Log.i(TAG, "onRecognitionResumed");
                  }
          };
  
  private void hotwordAvailabilityChangeHelper(int status) {
  
      Log.i(TAG, "Hotword availability = " + status);
      switch (status) {
          case AlwaysOnHotwordDetector.STATE_HARDWARE_UNAVAILABLE:
              Log.i(TAG, "STATE_HARDWARE_UNAVAILABLE");
              break;
          case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNSUPPORTED:
              Log.i(TAG, "STATE_KEYPHRASE_UNSUPPORTED");
              break;
          case AlwaysOnHotwordDetector.STATE_KEYPHRASE_UNENROLLED:
              Log.i(TAG, "STATE_KEYPHRASE_UNENROLLED");
              Intent enroll = mHotwordDetector.createEnrollIntent();
              Log.i(TAG, "Need to enroll with " + enroll);
              break;
          case AlwaysOnHotwordDetector.STATE_KEYPHRASE_ENROLLED:
              Log.i(TAG, "STATE_KEYPHRASE_ENROLLED - starting recognition");
              if (mHotwordDetector.startRecognition(0)) {
                  Log.i(TAG, "startRecognition succeeded");
              } else {
                  Log.i(TAG, "startRecognition failed");
              }
              break;
      }
  
      //    final static AlwaysOnHotwordDetector.Callback
  }}
  </code></pre>