The testing framework calls this handler method automatically when the framework is unable to find a match using the UiSelector. When a match is not found after a predetermined time has elapsed, the framework calls the checkForCondition() method of all registered watchers on the device. You can use this method to handle known blocking issues that are preventing the test from proceeding. For example, you can check if a dialog appeared that is blocking the the test, then close the dialog or perform some other appropriate action to allow the test to continue.

Returns
true to indicate a matched condition, or false if no matching condition is found

这是我直接从官方文档上扣下来的,简单来说,当你 Uiobject 对象无法匹配 Uiselector 条件时会触发当前所有已注册运行的监听器,避免测试中有类似意外弹出框导致终止运行.
以下就测试中来电挂断场景为例,各位测试同仁如遇困惑可参考.监听器必须在 case 前运行,一般在方法第一行注册this.watcherEndTheCall();

private void watcherEndTheCall(){
        UiDevice.getInstance().registerWatcher("endCall", new UiWatcher() {
            UiObject incomingCall = new UiObject(new UiSelector()
                                        .resourceId("com.android.phone:id/callStateLabel"));

            @Override
            public boolean checkForCondition() {
                System.out.println("watcherEndCall running");
                if(incomingCall.exists()){
                    try {
                        Runtime.getRuntime().exec("input keyevent 5");
                        sleep(1000);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println("endCall success");
                    return true;
                }
                // TODO Auto-generated method stub
                System.out.println("endCall fail");
                return false;

            }
        });
    }


↙↙↙阅读原文可查看相关链接,并与作者交流