0

我有一个 Galaxy Tab 1000。

我正在尝试编写一个代码,它将识别任何运动。

我的手机上有 2.2 版。

这是 oncreate 代码。


OnCreate()
    mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
        mySensorManager.registerListener(mySensorEventListener, mySensorManager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL); // 

这是传感器 onsensorchanged 实现(我认为非常简单)但不起作用


if (event.sensor.getType() == SensorManager.SENSOR_ACCELEROMETER) // Do WORK

event.sensor.getType() 返回 1

如果我这样做: getSensorList(Sensor.TYPE_ALL); 我得到传感器罗盘、罗盘、BMA150(加速度)、温度、磁铁、接近度、光和陀螺仪。

(奇怪 - 我以为它只在 3.0 中出现)

请指教,我怎样才能在手机上获得运动检测。

谢谢


4

1 回答 1

1

我对您到底想要做什么感到有些困惑,但从我的观点来看,您想在 onSensorChanged 中获取加速度数据。您的 on create 看起来不错,但是您可以通过检查从 registerListener 返回的布尔值来检查它们是否正确注册了侦听器。这是我在侦听事件并检索返回的值时所做的事情。

    @Override
    public void onSensorChanged(SensorEvent event)
    {
        // If the sensor data is unreliable return
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            //Toast.makeText(main.this, "Sensor Status Unreliable",Toast.LENGTH_SHORT).show();
            return;
        }


        // Gets the value of the sensor that has been changed
        switch (event.sensor.getType())
        {
        case Sensor.TYPE_ACCELEROMETER:
            m_vAccel = event.values.clone();
            break;
        }

否则,您也可以使用与加速度计返回负重力相同的线性加速度传感器。http://developer.android.com/reference/android/hardware/SensorEvent.html

于 2011-06-16T16:26:37.977 回答