Problem working with sensor - accelerometer
I have a Galaxy Tab 1000.
I am trying to write a code, that will identify ANY movement.
I have version 2.2 on the phone.
This is the oncreate code.
OnCreate()
mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mySensorManager.registerListener(mySensorEventListener, mySensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL); //
This is the sensor onsensorchanged implementation (very simple I think) but not working
if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER) // Do WORK
The event.sensor开发者_运维技巧.getType() returns 1
If I do: getSensorList(Sensor.TYPE_ALL); I get sensors compass, compass, BMA150 (accel), temp, magnet, proximity, light and Gyro.
(weird - I thought it's only up in 3.0)
Please advise, how can I get motion detection on the phone.
thanks
I am a little confused of what exactly it is that you want to do, but from what I take you want to get acceleration data in your onSensorChanged. Your on create looks fine, however you can check if they are registering the listener correctly by checking the bool that returns from the registerListener. Heres what i do when listening for an event and retrieving the values returned.
@Override
public void onSensorChanged(SensorEvent event)
{
// If the sensor data is unreliable return
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
//Toast.makeText(main.this, "Sensor Status Unreliable",Toast.LENGTH_SHORT).show();
return;
}
// Gets the value of the sensor that has been changed
switch (event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
m_vAccel = event.values.clone();
break;
}
Other wise you can also use the Linear Acceleration sensor which is the same as the accelerometer returns minus gravity. http://developer.android.com/reference/android/hardware/SensorEvent.html
精彩评论