Android Emulator Orientation Change Through Emulat

2020-06-06 05:00发布

问题:

I am trying to build an automated test framework for checking basic things that an app should respond to, without crashing. One of those is checking if an app responds to configuration change properly. Now, since it is automated, I have to switch the emulator's orientation between landscape and portrait and observe if it crashes, PROGRAMMATICALLY. [caps cuz there are many questions relating to ctrl+F12 or num pad 7]

I am trying to figure out an adb or emulator console command to get this done.

i did a telnet to localhost:5554 and entered the following commands:

$sensor status // gives the enabled/disabled status of all sensors

acceleration: enabled.
magnetic-field: enabled.
orientation: enabled.
temperature: enabled.
proximity: enabled.
OK

$sensor get orientation

orientation = 0:0:0

$sensor set orientation 90:0:0

OK

I tried different combinations of this and it doesn't respond. To get the value that is set when it is in landscape mode, I manually changed the orientation using ctrl+F12 and tried getting the orientation sensor status again, and to my dismay, the value didn't change.

A certain blog also mentioned this particular command in ADB

$service call window 18 i32 1

$service call window 18 i32 0

for switching orientations. But, I get this error message

Result: Parcel(fffffffc ffffffff '........')

It would be great if I could get a solution to this.

回答1:

Finally I figure out a way to do it.

First I will try to clarify the question a little bit:

usually people talking about 'rotate' the AVD screen actually mean changing the skin layout to portrait/landscape (Mimic rotation of the tablet). Which can be done by press "7/9" key on number keypad or press ctrl+Fn+F12. In this case, the direction of the display will not change, sometimes the display will be annoying Left-to-right or right-to-left, in stead of natural up-to-bottom. This can be solved by uncheck the auto-rotation in android setting.

However, in some case we would like to see the effect of the gravity sensor. I think this is actually related to the OP's question. Unlike the GPS, there is no GUI tools of Eclipse to do this. The OP is very close to solve the problem. However, the orientation sensor is not the correct one, we need to use the gravity/acceleration sensor to rotate the screen automatically. try following command and observe the display direction with auto-rotation on:

sensor set acceleration 9.77622:0:0
sensor set acceleration 0:9.77622:0
sensor set acceleration 0:0:9.77622