Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implement start and stop sensor android

Tags:

android

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?

like image 618
user788511 Avatar asked Jun 22 '11 05:06

user788511


People also ask

What is Sensor_delay_normal?

SENSOR_DELAY_NORMAL. rate (default) suitable for screen orientation changes.

What is the use of SensorManager in Android?

Here is an example demonstrating the use of SensorManager class. It creates a basic application that allows you to view the list of sensors on your device. To experiment with this example , you can run this on an actual device or in an emulator.

What is Android gyroscope?

Gyroscope in a smartphone provides a GUI that enables a user to select menus etc by tilting the phone. One can deflect the phone slightly to go up and down the contact list. It enables a smartphone to trigger preset commands basis different motions. For instance, one can shake the phone to lock it.


1 Answers

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

like image 91
vgonisanz Avatar answered Oct 05 '22 23:10

vgonisanz