我的初级和我正在研究React Native应用程序,该应用程序响应Android系统的调用。我们有两个类需要在下面显示的两个类之间进行通信:MyModule(第一个代码块),当Android系统中发生事件时,应该由OtherClass(第二个代码块)通知。
我们看到的是:
因此,我们尝试做的是允许我们的ReactNative应用程序在发生给定系统事件时从Android类接收消息。我们试图使用"教程"在http://facebook.github.io/react-native/docs/native-modules-android.html但是,因为这是在没有完整源代码的情况下提供的,所以我们有点迷失。
有什么建议吗?
谢谢!
OtherClass.java:
package com.ats;
import com.ats.SuperClass;
import com.ats.MyModule;
import android.content.Context;
import com.ats.MyModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.util.Date;
import javax.annotation.Nullable;
public class OtherClass extends SuperClass {
private ReactApplicationContext reactContext;
public OtherClass(ReactApplicationContext reactContext) {
super();
this.reactContext = reactContext;
}
public OtherClass() {
super();
}
@Override
protected void onEventTriggered(Context ctx, String number, Date start) {
WritableMap params = Arguments.createMap();
params.putInt("test", 0);
sendEvent("sendCall", params);
}
private void sendEvent(String eventName, @Nullable WritableMap params) {
System.out.println("OtherClass ReactAppContext: " + this.reactContext); //ReactContext is being lost at this point(Nullpointexeception)
this.reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, params);
}
}
...
MyModule.java:
package com.ats;
import com.ats.OtherClass;
import android.telecom.Call;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReadableMap;
import javax.annotation.Nullable;
/**
* Provides a bridge between JavaScript and Java
*/
public class MyModule extends ReactContextBaseJavaModule {
private OtherClass OR;
/**
* Our local copy of the React Application Context
*/
/**
* Construct an instance of this class and the OtherClass Java class
*
* @param reactContext
*/
public MyModule(ReactApplicationContext reactContext) {
super(reactContext);
this.OR = new OtherClass(reactContext);
}
/**
* Define the name of this ReactNative [Native] Module
*
* @return String
*/
@Override
public String getName() {
return "Module";
}
/**
* Perform a simple println to test whether this code is reachable
*/
@ReactMethod
public void print() {
System.out.println("React Method Print");
}
}