Use start and stop sensor

I am working on a sensor action on an iphone, and I was wondering if anyone would be able to find out how to implement sensor start and stop actions in android. I searched this forum but could not find anything final. Does anyone know and suggest?

+6
source share
2 answers

Using a sensor for activity is easy. Basically you need:

1ยบ Record the sensors you need.

// Sensor static static private SensorManager mSensorManager; static private List<Sensor> deviceSensors; static private Sensor mAccelerometer; static private Sensor mGravity; static private Sensor mGyroscope; static private Sensor mLinearAcceleration; static private Sensor mRotationVector; static private Sensor mOrientation; static private Sensor mMagneticField; static private Sensor mProximity; static private Sensor mPressure; static private Sensor mLight; 

2ยบ You should initialize everything onCreate, something like this:

  // Add sensor manager STATIC (only 1 time) mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); // Load default Sensors loadDefaultSensors(); // Set Sensor Listener setAllSensorListener(); 

3ยบ loadDefaultSensors is something like this:

  mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE); mLinearAcceleration = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); mRotationVector = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); mMagneticField = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); mPressure = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE); mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); 

And 4, set the listeners:

  mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mGravity, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mLinearAcceleration, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mRotationVector, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mMagneticField, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mPressure, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL); 

5ยบ If you need to save CPU, etc., you can use unlistener onPause and register onResume again

  mSensorManager.unregisterListener(this); 

I hope this helps to get started ..... Everything you need to know, HERE

+12
source

Source: https://habr.com/ru/post/891077/


All Articles