This code uses the different methods using SensorListener and SensorManager.
import android.content.Context;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
/**
* The accelerometer manager class
*/
public class AccelerometerManager implements SensorListener
{
// The unique instance of AccelerometerManager
private static AccelerometerManager m_oInstance = null;
// Object use to synchronize access to the data
private static Object m_oLock = new Object();
// Current acceleration on x, y and z axis
private static float m_fX;
private static float m_fY;
private static float m_fZ;
// Is the accelerometer manager started?
private static boolean m_bIsStarted = false;
/**
* Initialize the accelerometer manager
* CAUTION : This method must be called before any call to the getX/Y/Z methods
*/
public static void initialize()
{
if (m_bIsStarted)
{
return;
}
SensorManager oSensorManager = (SensorManager)Globals.m_oRichMediaDriver.m_oContext.getSystemService(Context.SENSOR_SERVICE);
m_oInstance = new AccelerometerManager();
oSensorManager.registerListener(m_oInstance, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_UI);
m_bIsStarted = true;
}
/**
* Release the accelerometer manager
* CAUTION : This method must be called when the accelerometer is not needed anymore,
* else the system would continue to send acceleration events uselessly
* @param oContext the Context
*/
public static void release(Context oContext)
{
if (!m_bIsStarted)
{
return;
}
SensorManager oSensorManager = (SensorManager)oContext.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
oSensorManager.unregisterListener(m_oInstance);
m_oInstance = null;
m_bIsStarted = false;
}
/**
* The onPause CB must be called when the Context is paused
* @param oContext the Context
*/
public static void onPause(Context oContext)
{
if (m_bIsStarted)
{
// Unregister listener
SensorManager oSensorManager = (SensorManager)oContext.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
oSensorManager.unregisterListener(m_oInstance);
}
}
/**
* The onPause CB must be called when the Context is resumed
* @param oContext the Context
*/
public static void onResume(Context oContext)
{
if (m_bIsStarted)
{
// register listener
SensorManager oSensorManager = (SensorManager)oContext.getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
oSensorManager.registerListener(m_oInstance, SensorManager.SENSOR_ACCELEROMETER, SensorManager.SENSOR_DELAY_UI);
}
}
/**
* @return the deci-g acceleration in the x axis
*/
public static int getX()
{
int nVal;
// Check if the accelerometer manager is already started
initialize();
synchronized(m_oLock)
{
nVal = (int)(100 * (m_fX/SensorManager.GRAVITY_EARTH)) ;
}
return nVal;
}
/**
* @return the deci-g acceleration in the y axis
*/
public static int getY()
{
int nVal;
// Check if the accelerometer manager is already started
initialize();
synchronized(m_oLock)
{
nVal = (int)(100 * (m_fY/SensorManager.GRAVITY_EARTH)) ;
}
return nVal;
}
/**
* @return the deci-g acceleration in the z axis
*/
public static int getZ()
{
int nVal;
// Check if the accelerometer manager is already started
initialize();
synchronized(m_oLock)
{
nVal = (int)(100 * (m_fZ/SensorManager.GRAVITY_EARTH)) ;
}
return nVal;
}
////////////////////////
// SensorListener part /
////////////////////////
public void onAccuracyChanged(int sensor, int accuracy)
{
}
public void onSensorChanged(int sensor, float[] values)
{
synchronized(m_oLock)
{
m_fX = values[SensorManager.DATA_X];
m_fY = values[SensorManager.DATA_Y];
m_fZ = values[SensorManager.DATA_Z];
}
}
}

