How can I properly control the scene orientation on Android using an AIR mobile phone?

I have an AIR application that I want to lock in landscape orientation. I never want the app to rotate to portrait mode. Adobe's solution for this problem would seemingly be to configure my * -app.xml file as follows:

<!-- The initial aspect ratio of the app when launched (either "portrait" or "landscape"). Optional. Mobile only. Default is the natural orientation of the device --> <aspectRatio>landscape</aspectRatio> <!-- Whether the app will begin auto-orienting on launch. Optional. Mobile only. Default false --> <autoOrients>false</autoOrients> 

This works most of the time, but not always. For example, in some versions of Android (example: 2.x, running on the Nook plank), if the screen of my device turns off when Flash Builder packs and deploys my APK, the application starts in a dilapidated state, where its aspect ratio is landscape, but everything is measured with portrait sizes. On other devices, such as Google Nexus (Android 4.1), the application starts correctly (in landscape mode), but if I turn off the screen, rotate the device in portrait mode and turn on the screen again - my AIR application was invisibly turned back to portrait mode. Even worse, StageOrientationEvent.ORIENTATION_CHANGE events are not even dispatched to indicate that this has happened.

I tried a variant of the above approach, which is to programmatically determine the orientation of the device:

 private function onAddedToStage( event:Event ):void { stage.autoOrients = false; stage.setOrientation( StageOrientation.ROTATED_RIGHT ); } 

This approach has the same problems discussed above.

I also tried the approach mentioned in this other thread . Instead of disabling auto orientation, I tried to listen to StageOrientationEvent.ORIENTATION_CHANGE and prevent the device from switching to portrait mode:

 private function onAddedToStage( event:Event ):void { stage.addEventListener( StageOrientationEvent.ORIENTATION_CHANGE, onStageOrientationChange, true, int.MAX_VALUE ); } private function onStageOrientationChange( event:StageOrientationEvent ):void { switch( event.afterOrientation ) { case StageOrientation.DEFAULT: case StageOrientation.UPSIDE_DOWN: case StageOrientation.UNKNOWN: event.preventDefault(); break; case StageOrientation.ROTATED_RIGHT: case StageOrientation.ROTATED_LEFT: break; } } 

This, too, seems to work only for a while. In many cases, event.preventDefault () has no effect, and the application rotates anyway.

So ... my question is to other AIR developers: does anyone know how to securely lock the device in one orientation?

+4
source share
1 answer

Here is the approach I found for me on Android 2.3 (Fire and Nook), as well as 4.1 (Nexus 7). I have not tested with a lot of devices yet, but so far it seems promising.

First, I set my -app.xml configuration as follows:

 <aspectRatio>landscape</aspectRatio> <autoOrients>false</autoOrients> 

Then I added the following code for my spark. Application:

 private function onAddedToStage():void { stage.addEventListener( Event.RESIZE, onStageResize ); NativeApplication.nativeApplication.addEventListener( Event.ACTIVATE, onNativeApplicationActivate ); } private function onStageResize( event:Event ):void { checkForOrientationChange(); } private function onNativeApplicationActivate( event:Event ):void { checkForOrientationChange(); } private function checkForOrientationChange():void { if ( height > width ) { if ( stage ) { stage.setOrientation( StageOrientation.ROTATED_RIGHT ); } else { // The first ACTIVATE event occurs before the Application has been added to the stage callLater( checkForOrientationChange ); } } } 
+4
source

Source: https://habr.com/ru/post/1432093/


All Articles