semtiko semtiko - 5 months ago 35
Android Question

Android live wallpaper service - get screen rotation

I try to make live wallpaper for android, but i can't get screen rotation, because lwp - it's a service, not activity. I need integer value (0/90/180/270), not orientation (landscape/portraite), like if i'm call something like this:

((Activity) context).getResources().getConfiguration().orientation;


It is possible? I tried many ways and read many articles, but i can't do it and can't find helpful information.

Thanks!

Answer

So, i found the solution :)

public int getRotation() {
    int orientation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();

    switch (orientation) {
        case Surface.ROTATION_90:  return 90;
        case Surface.ROTATION_180: return 180;
        case Surface.ROTATION_270: return 270;
        default: return 0;
    }
}

But in my case it's little bit buggy, don't know why yet: If you place your device to 0 degrees position (portrait orientation) and rotate to 180 degrees, nothing be detected, or from 90 to 270 and versa. But if you rotate your device from 0 to 90 or 270, or from 90 to 180 and 0 and so on - all is fine (different orientations).

Comments