如何在uiautomator下获得android手机屏幕尺寸

时间:2014-09-05 04:46:58

标签: android uiautomator

我想在android uiautomator测试中添加一个功能,即识别哪个是平板电脑,哪个是手机。

你知道,我们使用布局来获得屏幕大小,如小,普通,大,xlarge,xxlarge

这是我的尝试:

final Configuration config = Resources.getSystem().getConfiguration();

final int screenSizeCode = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;

String screenSize;

    switch (screenSizeCode) {

        case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:

            screenSize = "screen_size_undefined";
            break;
        case Configuration.SCREENLAYOUT_SIZE_SMALL:
            screenSize = "screen_size_small";
            break;
        case Configuration.SCREENLAYOUT_SIZE_NORMAL:
            screenSize = "screen_size_normal";
            break;
        case Configuration.SCREENLAYOUT_SIZE_LARGE:
            screenSize = "screen_size_large";
            break;
        case Configuration.SCREENLAYOUT_SIZE_XLARGE:
            screenSize = "xlarge";
            break;
        default:
            screenSize = "screen_size_unknown";
            break;
    }

结果是:

screenSize = "screen_size_undefined"

实际上我的手机屏幕尺寸正常

我想知道如何从uiautomator获取屏幕尺寸?

1 个答案:

答案 0 :(得分:0)

如果dp (device-independent pixel)中的显示尺寸可以帮助您找出设备类型,那么您可以使用

UiDevice device = UiDevice.getInstance();
Point p = device.getDisplaySizeDp();

要求:API等级18