0

I am using OrientationEventListener() for detecting orientation change but it gives continuous values.I want only four values for each orientation i.e PORTRAIT,REVERSE_PORTRAIT,LANDSCAPE AND REVERSE_LANDSCAPE MODE.what should i do to get values only if orientation changes.My problem is i want to detect orientation when device quickly turns from LANDSCAPE MODE to REVERSE LANDSCAPE MODE.I want to call currentScreenOrientation() function only once when orientation changes but now it is calling it for every value between 0 to 359.

        mOrientationEventListener = new OrientationEventListener(this,
                SensorManager.SENSOR_DELAY_NORMAL) {
            @Override
            public void onOrientationChanged(int arg0) {
                currentScreenOrientation();
            }
        };

        if (mOrientationEventListener.canDetectOrientation()) {
            Toast.makeText(this, "Can DetectOrientation", Toast.LENGTH_LONG)
                    .show();
            mOrientationEventListener.enable();
        } else {
            Toast.makeText(this, "Can't DetectOrientation", Toast.LENGTH_LONG)
                    .show();
            finish();
        }
    }

thanx in advance.

4

1 回答 1

1

应用启动时首先保存设备的方向

mDisplay = getWindowManager().getDefaultDisplay();
mCurrentOrientation = mDisplay.getRotation(); // return values are 0, 90, ... 

onOrientationChanged检查方向是否改变

@Override
public void onOrientationChanged(int orientation)
{

      if (mDisplay.getRotation() != mCurrentOrientation)
      {
          mCurrentOrientation = mDisplay.getRotation();
          currentScreenOrientation();
      }

 }
于 2013-04-26T16:12:06.237 回答