React Native Android模块不起作用

时间:2016-12-13 10:06:57

标签: javascript android react-native

我试图在React-Native中运行后台服务。从我所听到的,我需要用本机Java编写它并将其连接到react-native代码。我做了一个模块,它没有抛出任何错误,但它什么也没做。我已经尝试更改我的代码以显示简单的吐司,它甚至没有这样做。这是代码:

服务:

public class TestService extends Service {

double distance = 0.0;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Toast.makeText(getApplicationContext(), "Service started...", Toast.LENGTH_SHORT).show();
    final ReactContext reactContext = new ReactContext(getApplicationContext());
    new Timer().scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run(){
            WritableMap params = Arguments.createMap();
            distance+= 0.7;
            params.putDouble("distance", distance);
            sendEvent(reactContext, "updateDistance", params);
        }
    },0,1000);
    return START_STICKY;
}

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

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

模块:

public class ServiceModule extends ReactContextBaseJavaModule {
ReactContext reactContext;

public ServiceModule(ReactApplicationContext reactContext) {
    super(reactContext);
    this.reactContext = reactContext;
}

@ReactMethod
public void startTrackingService() {
    Intent intent = new Intent(reactContext, TestService.class);
    reactContext.startService(intent);
    Toast.makeText(getReactApplicationContext(), "Starting service...", Toast.LENGTH_SHORT).show();
}

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

包装:

public class ServicePackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new ServiceModule(reactContext));
    return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
    return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}
}
MainApplication中的

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new ReactNativePushNotificationPackage(),
      new ServicePackage()
  );
}

然后在react-native代码中 - ServiceModule.js:

import { NativeModules } from 'react-native';
module.exports = NativeModules.ServiceModule;

在Tracking.js中:

import ServiceModule from '../component/ServiceModule';
....
startTracking() {
    console.warn("Trying to start serivce");
    ServiceModule.startTrackingService;
}

componentWillMount() {
    DeviceEventEmitter.addListener('updateDistance', function(e: Event) {
        console.warn("got event");
        this.updateDistance(e.distance);
    });
}

updateDistance(newDistance) {
    this.setState({distance: newDistance});
}

console.warn("Trying to start service");正在显示,因此肯定会调用startTracking()方法;

1 个答案:

答案 0 :(得分:3)

startTracking() {
    console.warn("Trying to start serivce");
    ServiceModule.startTrackingService();
}