事件未发送到JavaScript

时间:2018-01-24 16:45:41

标签: javascript java android react-native android-sensors

我正在使用react-native来开发一个Android应用程序。我使用本机代码在前台运行服务。该服务包括手机的加速计传感器读数集。

启动服务会返回成功的承诺,但我没有收到任何事件。以下是服务类的实现:

$json = file_get_contents('php://input');

事情甚至没有出现通知,我担心服务根本没有启动。 以下是我使用此服务的模块:

    public class PhonePositionService extends Service {
      public static final String FOREGROUND = "com.testnative.position.FOREGROUND"; 
      ...
      //Event listener for sensors -start
      SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
          PhonePositionService.this.sendMessage(event);
        }
    ...
      };
      //Event Listener - end

      @Override
      @TargetApi(Build.VERSION_CODES.M)
      public void onCreate() {
        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); //get services provided by sensor manager
        mAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //specifiy sensor
        sensorManager.registerListener(sensorEventListener, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); //register it
      }

      private void sendMessage(SensorEvent event) {
        try {
          float[] values = event.values;

          Intent intent = new Intent("PhonePosUpdate");
          intent.putExtra("message", values);
          LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
...

当我从JavaScript调用startService()时,我获得了成功。但是没有出现通知和

public class PhonePositionModule extends ReactContextBaseJavaModule {
  public PhonePositionModule(ReactApplicationContext reactContext) {
    super(reactContext);
    BroadcastReceiver phonePositionReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        float[] message = intent.getFloatArrayExtra("message");
        PhonePositionModule.this.sendEvent(message);
      }
    };
    LocalBroadcastManager.getInstance(getReactApplicationContext()).registerReceiver(phonePositionReceiver, new IntentFilter("PhonePosUpdate"));
  }
...
  @ReactMethod
  public void startService(Promise promise) {
    String result = "Success";
    try {
      Intent intent = new Intent(PhonePositionService.FOREGROUND); ///////
      intent.setClass(this.getReactApplicationContext(), PhonePositionService.class);
      getReactApplicationContext().startService(intent);
    } catch (Exception e) {
      promise.reject(e);
      return;
    }
    promise.resolve(result);
  }
...
  private void sendEvent(float[] message) {
    WritableMap map = Arguments.createMap();
    map.putDouble("x", message[0]);
    map.putDouble("y", message[1]);
    map.putDouble("z", message[2]);
    getReactApplicationContext().getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("updatePosition", map);
  }
}
在App.js中的

什么都没有显示。

因此: 1)通知未出现 2)鉴于没有出现通知,服务没有启动(即使承诺没有返回错误)。

我尝试在Android Studio上调试本机代码,但logcat对我不起作用。

0 个答案:

没有答案