title: Android speech recognition stop after 3 times
body: <p>first sorry for my english, mi native language is spanish</p>
  
  <p>i follow this topics to implements speech recognition in backgroun via service <a href="https://stackoverflow.com/questions/14940657/android-speech-recognition-as-a-service-on-android-4-1-4-2/14950616#14950616">Android Speech Recognition as a service on Android 4.1 &amp; 4.2</a> and <a href="https://stackoverflow.com/questions/16219601/how-to-make-google-voice-run-continually-in-the-background-on-android">How to make Google Voice run continually in the background on Android</a></p>
  
  <p>in my code everithing works, but when i speak 3 words separately the service not listen more words or commands.</p>
  
  <p>the logcat is here </p>
  
  <pre><code>11-26 15:51:33.627: D/Icaro(29990): Servicio Voz segundo plano Apagado
  11-26 15:51:36.927: D/Icaro(29990): Servicio Voz segundo plano Encendido
  11-26 15:51:36.967: D/Icaro(29990): message start listening
  11-26 15:51:38.207: D/Icaro(29990): onBeginingOfSpeech
  11-26 15:51:39.547: D/Icaro(29990): google   --&gt; command
  11-26 15:51:39.547: D/Icaro(29990): onResults
  11-26 15:51:39.547: D/Icaro(29990): message start listening
  11-26 15:51:40.197: D/Icaro(29990): onBeginingOfSpeech
  11-26 15:51:42.187: D/Icaro(29990): facebook  --&gt; command
  11-26 15:51:42.187: D/Icaro(29990): onResults
  11-26 15:51:42.187: D/Icaro(29990): message start listening
  11-26 15:51:43.197: D/Icaro(29990): onBeginingOfSpeech
  11-26 15:51:44.947: D/Icaro(29990): whatsapp   --&gt; command 
  11-26 15:51:44.947: D/Icaro(29990): onResults
  11-26 15:51:44.947: D/Icaro(29990): message start listening
  11-26 15:51:46.787: D/Icaro(29990): onBeginingOfSpeech
  11-26 15:51:48.057: D/Icaro(29990): google   --&gt; command
  11-26 15:51:48.057: D/Icaro(29990): onResults
  11-26 15:51:48.067: D/Icaro(29990): message start listening
  </code></pre>
  
  <p>and the code of service is here </p>
  
  <pre><code>public class SpeechActivationService extends Service
      {
  protected AudioManager mAudioManager; 
  protected SpeechRecognizer mSpeechRecognizer;
  protected Intent mSpeechRecognizerIntent;
  protected final Messenger mServerMessenger = new Messenger(new IncomingHandler(this));
  protected boolean mIsListening;
  protected volatile boolean mIsCountDownOn;
  
  static String TAG = "Icaro";
  
  static final int MSG_RECOGNIZER_START_LISTENING = 1;
  static final int MSG_RECOGNIZER_CANCEL = 2;
  
  private int mBindFlag;
  private Messenger mServiceMessenger;
  
  @Override
  public void onCreate()
  {
      super.onCreate();
      mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
      mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
      mSpeechRecognizer.setRecognitionListener(new SpeechRecognitionListener());
      mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
      mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                                       RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
      mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
                                       this.getPackageName());
  
      //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
  }
  
  protected static class IncomingHandler extends Handler
  {
      private WeakReference&lt;SpeechActivationService&gt; mtarget;
  
      IncomingHandler(SpeechActivationService target)
      {
          mtarget = new WeakReference&lt;SpeechActivationService&gt;(target);
      }
  
  
      @Override
      public void handleMessage(Message msg)
      {
          final SpeechActivationService target = mtarget.get();
  
          switch (msg.what)
          {
              case MSG_RECOGNIZER_START_LISTENING:
  
                  if (Build.VERSION.SDK_INT &gt;= 16);//Build.VERSION_CODES.JELLY_BEAN)
                  {
                      // turn off beep sound  
                      target.mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
                  }
                   if (!target.mIsListening)
                   {
                       target.mSpeechRecognizer.startListening(target.mSpeechRecognizerIntent);
                       target.mIsListening = true;
                      Log.d(TAG, "message start listening"); //$NON-NLS-1$
                   }
                   break;
  
               case MSG_RECOGNIZER_CANCEL:
                    target.mSpeechRecognizer.cancel();
                    target.mIsListening = false;
                    Log.d(TAG, "message canceled recognizer"); //$NON-NLS-1$
                    break;
           }
     } 
  } 
  
  // Count down timer for Jelly Bean work around
  protected CountDownTimer mNoSpeechCountDown = new CountDownTimer(5000, 5000)
  {
  
      @Override
      public void onTick(long millisUntilFinished)
      {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onFinish()
      {
          mIsCountDownOn = false;
          Message message = Message.obtain(null, MSG_RECOGNIZER_CANCEL);
          try
          {
              mServerMessenger.send(message);
              message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
              mServerMessenger.send(message);
          }
          catch (RemoteException e)
          {
  
          }
      }
  };
  
  @Override
  public int onStartCommand (Intent intent, int flags, int startId) 
  {
      //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
      try
      {
          Message msg = new Message();
          msg.what = MSG_RECOGNIZER_START_LISTENING; 
          mServerMessenger.send(msg);
      }
      catch (RemoteException e)
      {
  
      }
      return  START_NOT_STICKY;
  }
  
  @Override
  public void onDestroy()
  {
      super.onDestroy();
  
      if (mIsCountDownOn)
      {
          mNoSpeechCountDown.cancel();
      }
      if (mSpeechRecognizer != null)
      {
          mSpeechRecognizer.destroy();
      }
  }
  
  protected class SpeechRecognitionListener implements RecognitionListener
  {
  
      @Override
      public void onBeginningOfSpeech()
      {
          // speech input will be processed, so there is no need for count down anymore
          if (mIsCountDownOn)
          {
              mIsCountDownOn = false;
              mNoSpeechCountDown.cancel();
          }               
          Log.d(TAG, "onBeginingOfSpeech"); //$NON-NLS-1$
      }
  
      @Override
      public void onBufferReceived(byte[] buffer)
      {
          String sTest = "";
      }
  
      @Override
      public void onEndOfSpeech()
      {
          Log.d("TESTING: SPEECH SERVICE", "onEndOfSpeech"); //$NON-NLS-1$
       }
  
      @Override
      public void onError(int error)
      {
          if (mIsCountDownOn)
          {
              mIsCountDownOn = false;
              mNoSpeechCountDown.cancel();
          }
           Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
           try
           {
               mIsListening = false;   
               mServerMessenger.send(message);
           }
           catch (RemoteException e)
           {
  
           }
           Log.d(TAG, "error = " + error); //$NON-NLS-1$
      }
  
      @Override
      public void onEvent(int eventType, Bundle params)
      {
  
      }
  
      @Override
      public void onPartialResults(Bundle partialResults)
      {
  
      }
  
      @Override
      public void onReadyForSpeech(Bundle params)
      {
          if (Build.VERSION.SDK_INT &gt;= 16);//Build.VERSION_CODES.JELLY_BEAN)
          {
              mIsCountDownOn = true;
              mNoSpeechCountDown.start();
              mAudioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
          }
          Log.d("TESTING: SPEECH SERVICE", "onReadyForSpeech"); //$NON-NLS-1$
      }
  
      @Override
      public void onResults(Bundle results)
      {
          ArrayList&lt;String&gt; data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
          Log.d(TAG, (String) data.get(0));
  
          //mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
  
          mIsListening = false;
          Message message = Message.obtain(null, MSG_RECOGNIZER_START_LISTENING);
          try
          {
                 mServerMessenger.send(message);
          }
          catch (RemoteException e)
          {
  
          }
  
          Log.d(TAG, "onResults"); //$NON-NLS-1$
      }
  
      @Override
      public void onRmsChanged(float rmsdB)
      {
  
      }
  }
  
  @Override
  public IBinder onBind(Intent arg0) {
      // TODO Auto-generated method stub
      return null;
  }
  }
  </code></pre>
  
  <p>How i can still detecting more commands?</p>