我正在尝试使用react native对文本识别进行一些演讲.我编写了一个react模块来启动识别器意图
public class SpeechToTextModule extends ReactContextBaseJavaModule {... @ReactMethod public voID startListening(Callback errorCallback, Callback successCallback) { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); if(getCurrentActivity() != null){ getReactApplicationContext().startActivityForResult(intent, SPEECH_REQUEST_CODE, null); } else { errorCallback.invoke(new NullPointerException("Activity is null")); }} public voID onActivityResult(int requestCode, int resultCode, Intent data){ // control must come here }
mainactivity看起来像这样:
public class MainActivity extends ReactActivity { ... @OverrIDe public voID onActivityResult(int requestCode, int resultCode, Intent data) { // how do i invoke the module function? }}
MainActivity从语音意图接收结果,但是我无法弄清楚如何将其传递给模块.
解决方法:
有一种方法可以达到register as Activity event listener.
将此添加到您的本机模块:
public class SpeechToTextModule extends ReactContextBaseJavaModule implements ActivityEventListener { public SpeechToTextModule(ReactApplicationContext reactContext) { super(reactContext); reactContext.addActivityEventListener(this); //Register this native module as Activity result Listener } @OverrIDe public voID onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { // Here is your Activity result :) } @OverrIDe public voID onNewIntent(Intent intent) { }}
总结 以上是内存溢出为你收集整理的android-将活动结果传递到react本机模块全部内容,希望文章能够帮你解决android-将活动结果传递到react本机模块所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)