Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java file can't find DeviceEventManagerModule when compiling debug with Java

I have been trying to write a module for react-native thats should call a Javascript method when the phone recevies a call. But when i run the command react-native run-android the compileDebugJavaWithJavac craches with the following error.

CallListenerModule.java:44 error: package DeviceEventManagerModule does not exist (DeviceEventManagerModule.RCTDeviceEventEmitter.class)

this is the CallListenerModule class:

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;


import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

import android.util.Log;

public class CallListenerModule extends ReactContextBaseJavaModule {
    BroadcastReceiverCustom broadcastRecevier;

    ReactContext context;

    public CallListenerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
        broadcastRecevier = new BroadcastReceiverCustom(reactContext);
    }

    @Override
        public String getName() {
        return "CallListenerModule";
    }

    public void sendCallEvent(String incomingNumber){
        WritableMap params = Arguments.createMap();
        params.putString("Number", incomingNumber);
        sendEvent(context, "CallRecevied", params);
    }


    private void sendEvent(ReactContext reactContext,
                        String eventName,
                        WritableMap params) {
        reactContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit(eventName, params);
    }
}

I have search the great internet for a solution for this problem but with no luck. The sendEvent method is copied from the docs. I removed the @Nullable from the params parameter because it casued another error and I do not intend on sending event without a parameter.

This is my first post on SO so any constructive criticism is appreciated :)

like image 363
Erik Ivares Avatar asked Dec 18 '22 17:12

Erik Ivares


1 Answers

You forgot to import the class com.facebook.react.modules.core.DeviceEventManagerModule. Therefore you can solve your problem by adding the following line:

import com.facebook.react.modules.core.DeviceEventManagerModule

like image 84
Robert Avatar answered Dec 21 '22 11:12

Robert