android中的onSensorChanged不断在模拟器中触发 [英] onSensorChanged in android continuously getting triggered in emulator
问题描述
我正在使用ACCELEROMETER传感器,并已通过
I am using ACCELEROMETER sensors and have registered a listener for the same via
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAcceleratorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mSensorManager.registerListener(this, mAcceleratorSensor , SensorManager.SENSOR_DELAY_NORMAL);
这是我的onSensorChanged的样子
This is how my onSensorChanged looks like
@Override
public final void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
switch(sensorType){
case Sensor.TYPE_ACCELEROMETER:
float valueX = event.values[0];
float valueY = event.values[1];
float valueZ = event.values[2];
Log.d(TAG, "Sensor Changed value:"+valueX+":"+valueY+":"+valueZ);
break;
}
但这是我在日志中看到的内容
However this is what I see in my logs
01-12 02:01:18.063 19691-19691/com.taxis.locationupdates2 D/LocationActivity: Sensor Changed value:2.0:2.0:2.0
01-12 02:01:18.129 19691-19691/com.taxis.locationupdates2 D/LocationActivity: Sensor Changed value:2.0:2.0:2.0
即使值没有变化,它也会无限循环地调用onSensorChanged.我还没有用真正的设备对其进行过测试.是否有任何可以控制的设置.
It keeps calling the onSensorChanged in infinite loop even though there is no change in the values. I havent tested it with a real device yet. Is there any settings to control the same.
推荐答案
该方法的命名很差.
From the android docs for onSensorChanged
:
在发生新的传感器事件时调用.请注意,更改后"是 有点用词不当,因为如果我们有一个新的 从具有完全相同的传感器值的传感器读取(但较新 时间戳).
Called when there is a new sensor event. Note that "on changed" is somewhat of a misnomer, as this will also be called if we have a new reading from a sensor with the exact same sensor values (but a newer timestamp).
这篇关于android中的onSensorChanged不断在模拟器中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!