Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read accelerometer data in Service with Android

Usually, in Android, we read the accelerometer data in an "Activity", by overriding the OnSensorChanged function.

I am curious how we can do it in a "Service".

Thanks, Vincent

like image 486
user523597 Avatar asked Nov 29 '10 07:11

user523597


Video Answer


2 Answers

Actually OnSensorChanged(SensorEvent event) is method from interface SensorEventListener.

So if you want to do it in service; you make your new class that extends Service and implements SensorEventListener.

like image 86
bbaja42 Avatar answered Oct 21 '22 07:10

bbaja42


You can do exactly the same in a Service as well.

public class BGHndlr extends Handler implements SensorEventListener {

    public BGHndlr(Looper looper) {
        super(looper);
     }

     @Override
     public void handleMessage(Message msg) {
         oSensorManager.registerListener(this, oAcceleroMeter, SensorManager.SENSOR_DELAY_FASTEST);
     }

    @Override
    public void onSensorChanged(SensorEvent event) {
         // do something
    }

    @Override
     public void onAccuracyChanged(Sensor sensor, int i) {}
}
like image 20
keinabel Avatar answered Oct 21 '22 08:10

keinabel