I create an application that starts the service when the "Start" button is pressed and stops it when the "Stop" button is pressed. in the service I register a listener for the ACCELEROMETER sensor to get the accelerometer values โโof the x, y, z axes .. but when I stop my application and cancel the recorder from the sensor, even then I get my accelerometer values.
Here is the code:
public class Accel extends Service
{
private static Context CONTEXT;
private static Sensor sensor;
private static SensorManager sensorManager;
private static boolean running = false;
@Override
public void onCreate()
{
}
@Override
public void onDestroy()
{
if (isListening())
stopListening();
}
@Override
public void onStart(Intent intent, int startid)
{
CONTEXT = this;
startListening(this);
}
public static Context getContext()
{
return CONTEXT;
}
public static boolean isListening()
{
return running;
}
public static void stopListening()
{
running = false;
sensorManager.unregisterListener(sensorEventListener, sensor);
}
public static void startListening(AccelerometerListener accelerometerListener)
{
sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0)
{
sensor = sensors.get(0);
running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
listener = accelerometerListener;
}
}
private static SensorEventListener sensorEventListener =
new SensorEventListener()
{
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event)
{
}
};
}
Can anyone help me out?
source
share