How to get ListnerRecognizer response in js function?

I am working on a ListnerRecognizer Listner.

I start the intent (startListning (intent)) from the webview button click click javascript

act.startFun(); 

And the startFun () method is declared in MainActivity.java

 public void startFun(){ Log.d(TAG,"ONCLICK"); Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test"); intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5); sr.startListening(intent); } 

and my SpeechRecognizer Listner is similar to

 class listner implements RecognitionListener{ public void onResults(Bundle results) { end=1; Log.d(TAG, "onResults"); } public void onRmsChanged(float rmsdB) { Log.d(TAG, "onRmsChanged"); } } 

My requirement is that I want to pass the result resultResults (Bundle results) to my javascript method.

Please help me achieve this.

0
source share
1 answer

There is no easy way to pass a variable to a JavaScript function ... but you could create a JavaScriptInterface class and tell JavaScript to get the string when you have it ready.

 webview.addJavascriptInterface(new JavaScriptInterface(),"Interface"); 

List the functions you want to call in the JavaScriptInterface class. Then in your javascript ...

 Interface.getStringResult(); 

You can also create a function to check if an interface exists in JavaScript:

 function runningInAndroidApp() { if (typeof Interface != 'undefined') { return true; } return false; } 
0
source

Source: https://habr.com/ru/post/1247962/


All Articles