简体   繁体   中英

How do I detect screen rotation on android?

i want to detect screen rotation via native.

for a example, when touch event occured, we can detect via /dev/input/event* event device

is this possible ?

i mean, without using java. just use native method.

WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
Display disp = wm.getDefaultDisplay();

int rotation = disp.getRotation(); // Android 2.2
Log.i( "Rotation", "rotation : " + rotation );

switch ( rotation )
{
    case Surface.ROTATION_0: 
        Log.i( "Roation", "Portrait : 0" ); 
        break;

    case Surface.ROTATION_90:
        Log.i( "Roation", "Landscape : 90" );
        break;

    case Surface.ROTATION_180:
        Log.i( "Roation", "Portrait : 180" );
        break;

    case Surface.ROTATION_270:
        Log.i( "Roation", "Landscape : 270" );
        break;
}

If you're using the NativeActivity infrastructure (it's not obvious from the question), there's a callback onConfigurationChanged in struct ANativeActivityCallbacks , which is made available to you as a part of a ANativeActivity object, which is pointed to by activity within the android_app structure, which is a parameter to your android_main .

Just reset the callback on startup to your function:

void OnConfig(ANativeActivity *ac)
{
    //...
}

state->activity->callbacks->onConfigurationChanged = OnConfig;

If you are indeed using NativeActivity, please tag the question with native-activity , not just android-ndk . There's enough difference of environment to warrant an extra tag.

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