如何让js函数SpeechRecognizer听者的反应? [英] How to get SpeechRecognizer listner response in js function?
问题描述
我的工作SpeechRecognizer听者的活动。
I am working on SpeechRecognizer Listner activity.
我从网页视图按钮点击触发的意图(startListning(意向))扔的JavaScript
I am triggering the intent (startListning(intent)) from Webview button click threw javascript
act.startFun();
而startFun()方法是在我MainActivity.java文件中声明
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);
}
和我SpeechRecognizer听者犹如
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");
}
}
我的要求是我想通过onResults(捆绑的结果)的结果字符串到我的JavaScript方法。
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.
推荐答案
有一个变量传递给JavaScript函数没有简单的方法......但是你可以
创建一个类JavaScriptInterface和信号JavaScript的,当你拥有它准备好字符串。
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");
列出要在JavaScriptInterface类调用的函数。
然后,在JavaScript ...
list the functions you want to call in the JavaScriptInterface class. Then in your javascript...
Interface.getStringResult();
您也可以创建一个函数来检查,看看是否在JavaScript中存在的接口:
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;
}
这篇关于如何让js函数SpeechRecognizer听者的反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!