2

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;
}
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
osmund sadler
  • 1,021
  • 2
  • 15
  • 27
  • You cannot access the /dev/input files from an application running on a secured device (ie a consumer device that has not been rooted). – Chris Stratton Oct 20 '12 at 14:15

1 Answers1

4

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.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281