title: Android Custom Keyboard with SpeechRecognizer
body: <p>I have a fully functional custom android keyboard in which i have to add speech recognition. Here are the relevant parts of the implementation i have</p>
  
  <pre><code>public class CustomInputMethodService 
      extends InputMethodService
      implements &lt;random stuff&gt; {
  
      private SpeechRecognizer mSpeechRecognizer;
      private RecognitionListener mSpeechlistener;
  
      public void onCreate() {
          super.onCreate();
          mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
          mSpeechlistener = new CustomRecognitionListener();
          mSpeechRecognizer.setRecognitionListener(mSpeechlistener);
      }
  
      @Override
      public void onPress(int primaryCode) {
          if (primaryCode == KeyCodes.VOICE_INPUT) {
              mSpeechRecognizer.startListening(getSpeechIntent());
          }else if(..){
              ...
          }
      }
  
      private Intent getSpeechIntent() {
          Intent speechIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
          speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
          speechIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, this.getPackageName());
          speechIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, false);
          return speechIntent;
      }
  
  }
  </code></pre>
  
  <p>The relevant method of the CustomRecognitionListener is simply:</p>
  
  <pre><code>        @Override
          public void onResults(Bundle results) {
              ArrayList&lt;String&gt; matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
              Log.d(TAG, "onResults: ----&gt; " + matches.get(0));
              if(matches != null &amp;&amp; matches.size() &gt; 0) {
                  writeText(matches.get(0));
              }
          }
  </code></pre>
  
  <p>This code is working just fine. The twist here is that i want a similar behaviour to what happens on google keyboard when the uset taps the microphone key:</p>
  
  <p><a href="https://i.stack.imgur.com/Qzl9U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qzl9U.png" alt="enter image description here"></a></p>
  
  <p>This would ideally by achieved by something like:</p>
  
  <pre><code>Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
  try {
      startActivityForResult(voiceIntent, Constants.RESULT_SPEECH);
  } catch (ActivityNotFoundException ex) {
      DebugLog.e(TAG, "Not found excpetion onKeyDown: " + ex);
  }
  </code></pre>
  
  <p>However, since the key listener is on and InputMethodService im not able to call startActivityForResult.
  What is the ideal way to accomplish this? Should i simply start a new activity without a layout and have a callback to the inputMethodService? seems messy</p>