注册传感器TYPE_ORIENTATION的侦听器后,我无法注销它,以便它停止报告ORIENTATION(软件类型传感器)的更改。我知道TYPE_ORIENTATION已过时。只是此传感器类型的ID“ 3”。
How can i replace type_orientation (is deprecated) for android 4.0.3?
我能够注册和注销其他任何东西(加速器,旋翼机,旋转矢量...)
Sensor orientation = getSensor(Sensor.TYPE_ORIENTATION + ""); // a method to loop through all the sensor existing in device and return the one I'm looking for
Log.d("TEST_ACTIVITY", "unregistering sensor " + orientation.getStringType());
if (orientation != null)
{
//it does reach here and call unregister this sensor
mSensorManager.unregisterListener(this, orientation);
}
//All sensors are registered when service starts
for (Sensor sensor : allSensors)
{
mSensorManager.registerListener(this, sensor, 500000, 500000);
}
答案 0 :(得分:0)
首先:您正在检查“ Sensor.TYPE_ORIENTATION”是否未包含在“ allSensors”列表中,或者正在代码中的某个时刻将该传感器添加到列表中。
第二:由于不建议使用“ Sensor.TYPE_ORIENTATION”,因此我建议使用“ OrientationEventListener”,代码如下:
OrientationEventListener orientationEventListener = new OrientationEventListener(this)
{
@Override
public void onOrientationChanged(int orientation)
{
Log.e(TAG, "orientation = " + orientation);
}
};
orientationEventListener.enable();
并停止监听器:
if(orientationEventListener != null) {
orientationEventListener.disable();
}
orientationEventListener = null;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
希望对您有所帮助:D!