本文介绍了android中的onSensorChanged不断在模拟器中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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.

推荐答案

该方法的命名很差.

来自 onSensorChanged的android文档:

From the android docs for onSensorChanged:

这篇关于android中的onSensorChanged不断在模拟器中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 01:25