Managed finally to determine android device orientation via JNI lazarus / 43-Android ---------------------------------------------------------------------- stab · 2013-04-12 11:46 Here is my code to determine orientation: function TfrmJNITestMain.GetRotation : LongInt; var   windowManagerClass, displayClass : jclass;   javaMethodId_WindowService, javaMethodId_getDefaultDisplay, javaMethodId_getRotation : JMethodID;   windowManagerObject, displayObject: JObject;   javaString_WINDOW_SERVICE, javaString_getDefaultDisplay : JString; begin   try     // get windowManagerClass     windowManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/WindowManager');     // get displayClass     displayClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/Display');     // get the string Context.WINDOW_SERVICE remember that NewStringUTF does not require ReleaseStringUTFChars     javaString_WINDOW_SERVICE := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getWindowManager'));     // get method id for WindowManager object     javaMethodId_WindowService := javaEnvRef^^.GetMethodID(javaEnvRef, javaAndroidAppActivityClass, 'getWindowManager', '()Landroid/view/WindowManager;');     // Get the WindowManager object     // Window w = (Window) getSystemService(Context.WINDOW_SERVICE);     windowManagerObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, javaActivityObject, javaMethodId_WindowService);     javaString_getDefaultDisplay := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getDefaultDisplay'));     // Get method id for Display object     javaMethodId_getDefaultDisplay := javaEnvRef^^.GetMethodID(javaEnvRef, windowManagerClass, 'getDefaultDisplay', '()Landroid/view/Display;');     // Get the display object     displayObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, windowManagerObject, javaMethodId_getDefaultDisplay);     // Get method id for method: getRotation     javaMethodId_getRotation := javaEnvRef^^.GetMethodID(javaEnvRef, displayClass, 'getRotation', '()I');     // Now call getRotation method in the display object     Result := javaEnvRef^^.CallIntMethod(javaEnvRef, displayObject, javaMethodId_getRotation);     debugln('rotation: ' + IntToStr(Result));   except     on E:Exception do       debugln('Error:'+ E.Message);   end; end; stab ---------------------------------------------------------------------- BigChimp · 2013-04-12 11:59 Ehm yes. Are you going to post a patch to the bugtracker with that code then ---------------------------------------------------------------------- stab · 2013-04-12 12:05 Think I'll first wait for a comment from felipemdc. I guess he'll be back tonight(that is swedish time) ---------------------------------------------------------------------- BigChimp · 2013-04-12 12:15 Good plan ---------------------------------------------------------------------- felipemdc · 2013-04-12 12:18 nice, what was exactly the problem? I tried here but wasn't able to pin point it ... JNI can be really tricky =) ---------------------------------------------------------------------- felipemdc · 2013-04-12 12:21 The try..except is useless, by the way. I tryed already to capture JNI exceptions and transform them into Pascal exception but JNI simply does not provide enough exception support APIs for that. If it is possible, I couldn't find an way with the existing JNI routines. You can even capture an exception, but after that all of JNI, except a routine to dump a standard error information to logcat, stops working, so you cannot get any kind of information about the exception =( You also cannot recover from it ... ---------------------------------------------------------------------- felipemdc · 2013-04-12 13:01 It is not easy to get right the way to merge this to the LCL, so I did it myself. You can see here how it is done, in case you want to learn: http://svn.freepascal.org/cgi- bin/viewvc.cgi?view=revision&root=lazarus&revision=40797 Now one can read the screen orientation like this: uses LazDeviceAPIs; procedure Tform1.btnProgressClick(Sender: TObject); var   Rotation: TScreenRotation; begin   Rotation := Device.GetScreenRotation(0); ---------------------------------------------------------------------- stab · 2013-04-12 13:23 My call to get WindowManager object was not right, even though the object was not nil. When I changed to:     // get method id for WindowManager object     javaMethodId_WindowService := javaEnvRef^^.GetMethodID(javaEnvRef, javaAndroidAppActivityClass, 'getWindowManager', '()Landroid/view/WindowManager;');     // Get the WindowManager object     // Window w = (Window) getSystemService(Context.WINDOW_SERVICE);     windowManagerObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, javaActivityObject, javaMethodId_WindowService); I got a valid WindowManager object and than it all worked. Thanks for your help and link to your merge to LCL