public void start() { // 模拟器下启动时键盘模拟重力 if (LSystem.isEmulator()) { _state._isConnected = true; sensorProcess = new SensorProcess(accelerometerValues); RealtimeProcessManager.get().addProcess(sensorProcess); return; } if (!_state._isConnected && manager == null) { manager = (SensorManager) LSystem.getActivity().getSystemService(Context.SENSOR_SERVICE); if (manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size() == 0) { _state._isConnected = false; } else { Sensor accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0); accelerometerListener = new SensorListener(this.accelerometerValues, this.magneticFieldValues); _state._isConnected = manager.registerListener( accelerometerListener, accelerometer, SensorManager.SENSOR_DELAY_GAME); } // 如果无法正常启动,则开启伪重力感应 if (!_state._isConnected) { _state._isConnected = true; sensorProcess = new SensorProcess(accelerometerValues); RealtimeProcessManager.get().addProcess(sensorProcess); } } }
public void stop() { if (manager != null) { if (accelerometerListener != null) { manager.unregisterListener(accelerometerListener); accelerometerListener = null; } manager = null; _state._isConnected = false; } else { _state._isConnected = false; } }