当前,以下代码确实成功获取了正在共享给应用程序的文本,但是我很难找到如何将该事件发送到Javascript端。我尝试创建一个新的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
}
}
}
将此事件发送到Javascript的正确方法是什么?
该应用具有react-native-navigation
,并且有很多对事件发射器的引用,我只是不确定如何获取反应上下文来发射事件。如果这样的话。
答案 0 :(得分:1)
我能够将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
,然后已初始化react上下文,并且无论应用程序是否已打开,我的事件都会启动。
答案 1 :(得分:0)
您可以使用webView.addJavascriptInterface()
您可以找到文档here。