简体   繁体   中英

Android: how to ignore rotation around single axis?

I'm developing application, where activity starts after phone's rotation. When you rotate phone backward that activity have to finish. Is it possible to ignore rotation around other axes?

You can check which way the phone is held with this:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

Then you can check the orientation like this:

switch (rotation)
{
    case Surface.ROTATION_90:
        ...
        break;
    case Surface.ROTATION_180:
        ...
        break;
    case Surface.ROTATION_270:
        ...
        break;
    default:
        ...
        break;
}

(Note that for pre-API 8 versions you'd need to use display.getOrientation() instead of display.getRotation() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM