How to get SpeechRecognizer listner response in js function?

淺唱寂寞╮ 提交于 2019-12-08 09:21:25

问题


I am working on SpeechRecognizer Listner activity.

I am triggering the intent (startListning(intent)) from Webview button click threw javascript

act.startFun();

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

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 like

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 i want to pass the onResults(Bundle results) 's result string to my javascript method.

Please some one help me to achieve this.


回答1:


There's no easy way to pass a variable to a JavaScript function... but you could create a class JavaScriptInterface and signal the 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 to see if the interface exists in JavaScript:

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


来源:https://stackoverflow.com/questions/11105937/how-to-get-speechrecognizer-listner-response-in-js-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!