开发者

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


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.


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) {}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜