How to implement custom Speech Recognition Intent


Hi guys,

I’m trying to implement speech recognition and I have problems in trying to implement it.

I found an implementation that seemingly fits my needs
but I don’t know how to implement it Nativescript.

I have a few questions regarding the implementation of the code and doing it in Typescript:

How do I pass in the app context?
I tried passing in, but it only crashes the app.
How do I implement this line of code?
class CustomRecognitionListener implements RecognitionListener

Those are the ones I can’t figure out for now although I am still clueless about Native APIs and would highly appreciate your help in implementing a custom Speech Recognition Intent.

Thank you


… but it only crashes the app.

Is not enough information to work on. How does it crash? Is there a particular exception message? Are there any logs?

See if the article at clears up some of the questions you might have.

How do I implement this line of code?

You need to dive a little into the Android reference to see what classes are exposed, what their namespaces are (the full lengthy dot-separated names).

A simple Google search for RecognitionListener yielded this ->

Now it becomes clear that the RecognitionListener is an interface, and I see all its methods that I would have to implement.

Depending on whether you want to implement the interface directly or extend a class, and then implement the interface you could do the following in JavaScript:


var myCustomRecognitionListener = new android.speech.RecognitionListener({
   onBeginningOfSpeech: function() { },
   onEndOfSpeech: function() { },

or Extend:

var CustomRecognitionListener = java.lang.Object.extend("my.custom.namespace.CustomRecognitionListener", {
   interfaces: [android.speech.RecognitionListener],
   onBeginningOfSpeech: function() { },
   onEndOfSpeech: function() { },
   // any other custom implementation  you'd like to bring along from the extended class
   // in this simple example - the Object class

Note that it is important to specify the full namespaces to classes/interfaces when calling to Android/Java api, you can normally save those in constant variable for easier access, like

const RecognitionListener = android.speech.RecognitionListener