聽起來好像你需要的代碼只有當設備的方向改變爲4個正常方向之一而不是每個角度時纔會有反應。這將定向篩選爲0,90,180和270度只值:
OrientationEventListener myOrientationEventListener;
int iOrientation;
myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL)
{
@Override
public void onOrientationChanged(int iAngle)
{ // 0 15 30 45 60 75, 90 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255, 270, 285, 300, 315, 330, 345
final int iLookup[] = {0, 0, 0,90,90, 90,90, 90, 90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments
if (iAngle != ORIENTATION_UNKNOWN)
{
int iNewOrientation = iLookup[iAngle/15];
if (iOrientation != iNewOrientation)
{
iOrientation = iNewOrientation;
// take action on new orientation here
}
}
}
};
// To display if orientation detection will work and enable it
if (myOrientationEventListener.canDetectOrientation())
{
Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG).show();
myOrientationEventListener.enable();
}
else
{
Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG).show();
}
我不得不改變查找值有點爲它與我的硬件正常工作,但這個效果很好。我希望有一個更好的方式來做到這一點,但在那之前它正在做它的工作。謝謝! – nrflaw 2014-01-05 14:05:24
誰曾發佈這是一個G!我在getResources()。getConfiguration()。orientation中沒有任何幫助的時候遇到了這個問題 – Matt 2014-09-05 19:47:36