0

I use accelerometer in my application, but on several devices senor axes is different (depends on default device orientation is portrait or landscape). in AndroidMaifest.xml:

<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
...
android:screenOrientation="portrait"

How can i get default device orientation from native code?

V-X
  • 2,979
  • 18
  • 28
Artem Romanov
  • 166
  • 1
  • 14

2 Answers2

1

Check the current orientation of the device in your OnCreate() (or the function where you do your initialization) and then based on the orientation set the axis for your sensor.

Follow the following links to see how to get the current orientation of the android device:

Check orientation on Android phone

how to detect orientation of android device?

Hope this help. If not, then please comment to share further details of your issue.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
0

If someone is still interested in a pure native solution to get the initial/default screen orientation, please use the code below.

For more information about the associated JAVA method:

https://developer.android.com/reference/android/view/Display.html#getRotation()

int get_inital_screen_orientation(struct android_app * app){


    JavaVM *lJavaVM = app->activity->vm;
    JNIEnv *lJNIEnv = app->activity->env;
    jobject n_instance = app->activity->clazz;

    lJavaVM->AttachCurrentThread(&lJNIEnv, 0);

    int rotation = -1;

    if (lJNIEnv) {
        jclass c_clazz = lJNIEnv->GetObjectClass(n_instance);
        jclass c_windowManager = lJNIEnv->FindClass("android/view/WindowManager");
        jclass c_display = lJNIEnv->FindClass("android/view/Display");
        jmethodID getWindowManager = lJNIEnv->GetMethodID(c_clazz, "getWindowManager", "()Landroid/view/WindowManager;");
        jmethodID getDefaultDisplay = lJNIEnv->GetMethodID(c_windowManager,"getDefaultDisplay","()Landroid/view/Display;");
        jmethodID getRotation = lJNIEnv->GetMethodID(c_display, "getRotation", "()I");
        jobject windowManager = lJNIEnv->CallObjectMethod(n_instance, getWindowManager);
        jobject display = lJNIEnv->CallObjectMethod(windowManager, getDefaultDisplay);
        rotation = lJNIEnv->CallIntMethod(display, getRotation);
        lJavaVM->DetachCurrentThread();
    }
    return rotation;

}
zerodebug
  • 11
  • 2
  • Is there no way to read this value directly from `NativeActivity` or `ANativeWindow`, using some `NDK` API? – l33t Jan 03 '23 at 22:56