16

I have a app idea in mind, but I haven't been able to figure out one part yet. In Google Now on Android, it gives you the following options to search:

  1. Type to search
  2. Click microphone to say your search
  3. Or just Say "Google" to make the voice search begin.

I'd like to know how they made #3 work. I need to have a keyword, that when said, initiates the voice search.

I imagine they have some type of background running service constantly listening for voice actions. Does anyone know how this is achieved?

Here is a image of the Google Now app:

enter image description here

Kamilion
  • 545
  • 4
  • 8
Kent Andersen
  • 2,151
  • 3
  • 21
  • 29
  • You want to know how they make it continuous or you want to make Google Now continuous. – Hoan Nguyen Apr 25 '13 at 16:47
  • 2
    I want to know how to do what they did, but for my own app. So, in other words, how can i pick up a keyword someone may have said....but without ever having to click on the microphone button – Kent Andersen Apr 25 '13 at 17:04

1 Answers1

4

You can write a service and then use createSpeechRecognizer in that service. For a complete service code for this you can find the answer at Android Speech Recognition as a service on Android 4.1 & 4.2

Community
  • 1
  • 1
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • 2
    Thanks so much! I had to tweak it just a little, but it worked like a charm! – Kent Andersen Apr 25 '13 at 19:06
  • I also posted a code for bluetooth headset. If you want to use it you can find it at my answer at http://stackoverflow.com/questions/14991158/using-the-android-recognizerintent-with-a-bluetooth-headset/14993590#14993590 – Hoan Nguyen Apr 25 '13 at 19:14
  • I'm still seeing a problem with Jellybean devices. After OnResults() is called, I call mSpeechRecognizer.startListening(mSpeechRecognizerIntent); Which should let me continue listening for more voice commands. On Jellybean after a few commands it just stops and onBeginningOfSpeech is never called again until I restart the app...whereas versions less than Jellybean work...any ideas? – Kent Andersen Apr 26 '13 at 19:45
  • sorry...i hit enter thinking it would do a newline...not submit...I've updated the comment above – Kent Andersen Apr 26 '13 at 19:52
  • 1
    Instead of mSpeechRecognizer.startListening(mSpeechRecognizerIntent); You should send the MSG_RECOGNIZER_START_LISTENING to the message handler. Read the question in that link and you see why we need a work around. Also you should send MSG_RECOGNIZER_START_LISTENING on error. – Hoan Nguyen Apr 26 '13 at 19:58
  • I edited my answer at the above link to send a message instead of calling startListening – Hoan Nguyen Apr 26 '13 at 20:09
  • That still doesn't work. When you send the message MSG_RECOGNIZER_START_LISTENING the variable "mIsListening" is still true. So it never calls "startListening". I then tried forcing the variable to false to see what would happen...and the same problem persisted. It stops listening. – Kent Andersen Apr 26 '13 at 20:27
  • @KentAndersen if you got this to work can you please post a sample code? am trying something similar as well. – aditya Oct 15 '14 at 01:09
  • @aditya yes. I did. But just by using the supplied link in the answer. I ultimately turned off the feature because it was a major battery drain. – Kent Andersen Oct 15 '14 at 01:16
  • okay. Did you manage to get a work around to integrate voice recognition in your app? am just looking for a sample working code because with the posted answer am running into lot of errors :( – aditya Oct 15 '14 at 01:19
  • Hey, I've been looking at the code in the answer...and I'm really confused by it, especially `IncomingHandler.class`. Can you explain a little bit about what is happening there? I've been trying for almost 3 hours now! Thanks so much! :) – Ruchir Baronia Jan 16 '16 at 23:37
  • The speech recognizer methods has to be called in the UI thread. Thus you need the IncomingHandler class since handleMessage method is called in the UI thread. – Hoan Nguyen Jan 17 '16 at 05:06
  • Can I use speech recognizer in a separate service in a separate thread? Thanks! – Ruchir Baronia Feb 05 '16 at 01:32
  • Anyone know if you can do this in ios? – justdan0227 Jun 25 '16 at 02:07