确定Android何时完成UI更新

时间:2015-11-27 14:35:29

标签: android

我使用ActivityInstrumentationTestCase2来测试我的UI。我的测试将改变屏幕上显示的某些值。但是,在设置值之后,我会通过拍摄快照图像来测试屏幕,在位图上运行校验和并将校验和值与预期值进行比较。但在设置UI值后,Android尚未完成UI的更新。我所知道的唯一解决方案是使用延迟等待几秒钟,虽然这是不可取的,因为它有不必要的等待时间,加起来有足够的测试。有没有办法知道Android何时实际更新了我的UI?

1 个答案:

答案 0 :(得分:0)

做了一些研究我发现了这个。还没试过呢:

  

要获得有关系统UI可见性更改的通知,请注册   View.OnSystemUiVisibilityChangeListener到您的视图。这是   通常是您用来控制导航可见性的视图。

     

例如,您可以将此代码添加到您的活动的onCreate()   方法:

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

http://developer.android.com/training/system-ui/visibility.html