I am working on the sensor action in 开发者_开发知识库iphone and I was wondering if anyone would know how to implement the start and stop sensor actions in android. I have searched this forum but not been able to find something definitive. Does anyone hae and clues?
Use sensor on an activity is easy. Basically you need:
1º Declarate 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 must initializate all 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 it 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 save CPU, etc, you can use a unlistener onPause and register again onResume
mSensorManager.unregisterListener(this);
I hope it help to start..... All you need to know, HERE
精彩评论