デバイスの向きがすべての角度ではなく4つの通常の向きのいずれかに変更された場合にのみ反応するようにする必要があるようです。
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
誰がこれを投稿した人はGです!私はgetResources()。getConfiguration()の助けを借りずにしばらくこの問題に立ち往生していました。 – Matt