如何在 React Native 中将事件从 MainActivity.java 发送到 Javascript?
How can I send an event from MainActivity.java to Javascript in React Native?
目前,以下代码确实成功获取了正在共享到应用程序的文本,但我无法找到如何将此事件发送到 Java 脚本端。我尝试创建一个新的 NavigationReactGateway
,但我的应用程序崩溃了。我是 Java 的新手,所以我什至不知道我是否做得对。
public class MainActivity extends SplashActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
protected String getMainComponentName() {
return "MyApplication";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Send event to Javascript as "share" event
}
}
}
将此事件发送到 Java脚本的正确方法是什么?
该应用程序有 react-native-navigation
并且有很多对事件发射器的引用,我只是不确定如何让反应上下文发出事件。如果这有任何意义。
您可以使用webView.addJavascriptInterface()
您可以找到文档 here。
我能够将 handleSendText
方法修改为以下内容以发送事件。
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
ReactContext context = NavigationApplication.instance.getReactGateway().getReactContext();
if (context != null) {
WritableMap params = Arguments.createMap();
params.putString("event", sharedText);
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("share", params);
}
}
}
我还必须从使用 onCreate
方法更改为使用 onStop
,然后反应上下文已经初始化并且我的事件启动无论应用程序是否已经打开。
目前,以下代码确实成功获取了正在共享到应用程序的文本,但我无法找到如何将此事件发送到 Java 脚本端。我尝试创建一个新的 NavigationReactGateway
,但我的应用程序崩溃了。我是 Java 的新手,所以我什至不知道我是否做得对。
public class MainActivity extends SplashActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
protected String getMainComponentName() {
return "MyApplication";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Send event to Javascript as "share" event
}
}
}
将此事件发送到 Java脚本的正确方法是什么?
该应用程序有 react-native-navigation
并且有很多对事件发射器的引用,我只是不确定如何让反应上下文发出事件。如果这有任何意义。
您可以使用webView.addJavascriptInterface()
您可以找到文档 here。
我能够将 handleSendText
方法修改为以下内容以发送事件。
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
ReactContext context = NavigationApplication.instance.getReactGateway().getReactContext();
if (context != null) {
WritableMap params = Arguments.createMap();
params.putString("event", sharedText);
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("share", params);
}
}
}
我还必须从使用 onCreate
方法更改为使用 onStop
,然后反应上下文已经初始化并且我的事件启动无论应用程序是否已经打开。