How to read accelerometer data in Service with Android

為{幸葍}努か 提交于 2019-12-23 11:52:53

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/4301636/how-to-read-accelerometer-data-in-service-with-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!