Set emulator/device orientation programmatically i

2019-04-08 00:32发布

In my instrumentation tests I want to test something in both landscape and portrait mode, so I'd like to set the orientation before the tests start. Is there a way to set the device or emulator orientation programmatically?

I am aware of the setRequestedOrientation() method but this works for a certain activity, if another activity is started I have to remember to call it again. What I'm looking for is a way to set the orientation "globally", so that every new activity is automatically started with that orientation.

UPDATE:

The solution must fit 2 requirements: 1) it doesn't make me change my production code, 2) it needs to run in a CI environment.

3条回答
我想做一个坏孩纸
2楼-- · 2019-04-08 00:48

You can do it for all of your activities by making a own AbtractActivity Class.

public abstract class AbstractActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Now you have to inherit all your activities from this class.

查看更多
劳资没心,怎么记你
3楼-- · 2019-04-08 00:51

You can start two emulator simultaneously one in potrait mode and one in landscape mode. To change emulator orientation use ctrl + f12.

查看更多
Fickle 薄情
4楼-- · 2019-04-08 01:00

Here is a ViewAction I created for making this simpler: https://gist.github.com/nbarraille/03e8910dc1d415ed9740

The usage is described in the comments. Hope that helps.

查看更多
登录 后发表回答