My junior and I are working on a React Native application which responds to calls made by the Android system. We have two classes which need to communicate between the two classes shown below: MyModule (the first code block), which should be notified by OtherClass (the second code block) when an event happens in the Android system.
What we're seeing that is:
- We create an instance of MyModule, which extends ReactContextBaseJavaModule, where we pass the ReactApplicationContext in via its Constructor.
- This creates an instance of OtherClass and passes the ReactApplicationContext into OtherClass, again via its constructor.
- In OtherClass, the method onEvent is called when a certain system event is fired. When this happens, OtherClass' private variable reactContext becomes null.
So what we're trying to do is allow our ReactNative application to receive messages from an Android class when a given system event happens. We've attempted to use the "tutorial" at http://facebook.github.io/react-native/docs/native-modules-android.html but, because this is provided without full source code, we're getting a bit lost.
Any suggestions please?
Thanks!
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");
}
}