Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 29454

How to get ReactContext in a Android Service?

$
0
0

I want to start my service from ReactNative code. Here is the module I use to bridge.

public class HeartbeatModule extends ReactContextBaseJavaModule {
    public static final String REACT_CLASS = "Heartbeat";
    private static ReactApplicationContext reactContext;

    public HeartbeatModule(@Nonnull ReactApplicationContext reactContext) {
      super(reactContext);
      this.reactContext = reactContext;
    }

    @Nonnull
    @Override
    public String getName() {
      return REACT_CLASS;
    }

    @ReactMethod
     public void startService() {
     // Starting the heartbeat service

     this.reactContext.startService(new Intent(this.reactContext, HeartbeatService.class));
     }


    @ReactMethod
    public void stopService() {
        // Starting the heartbeat service

        this.reactContext.stopService(new Intent(this.reactContext, HeartbeatService.class));
    }
}

My question is how can I access this reactContext in my service. And how to sustain it when the service restarts.


Viewing all articles
Browse latest Browse all 29454

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>