Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android UiAutomator锁定设备并停止响应adb命令_Android_Android Uiautomator_Nanohttpd - Fatal编程技术网

Android UiAutomator锁定设备并停止响应adb命令

Android UiAutomator锁定设备并停止响应adb命令,android,android-uiautomator,nanohttpd,Android,Android Uiautomator,Nanohttpd,我目前正试图通过UIautomator创建一个非常简单的UI转储程序,但有一些非常奇怪的问题。在我目前的设计中,我使用nanohttpd与我的apk通信 因此,我的uiautomator测试如下所示: @RunWith(AndroidJUnit4.class) @SdkSuppress(minSdkVersion = 18) public class Start { public static final String Tag = "UiDuper"; private int

我目前正试图通过UIautomator创建一个非常简单的UI转储程序,但有一些非常奇怪的问题。在我目前的设计中,我使用nanohttpd与我的apk通信

因此,我的uiautomator测试如下所示:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {

    public static final String Tag = "UiDuper";
    private int port;
    private UiAutomatorHttpServer server;

    public Start() {
        Bundle extras = InstrumentationRegistry.getArguments();
        port = 9008;
        if(extras.containsKey("port")) {
            port = Integer.parseInt(extras.getString("port"));
            Log.i(Tag, "UIAutomator server port is " + port);
        }

    }

    @Before
    public void setUp() throws IOException, RemoteException {
        Log.i(Tag, "Start server with port " + port);
        server = new UiAutomatorHttpServer(port);
        server.start();
    }

    @After
    public void TearDown() {
        Log.i(Tag, "Tear down server");
        if(server != null) {
            server.stop();
        }
    }

    @Test
    @LargeTest
    public void RunServer() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            if(server == null)
                return;
            if(!server.isAlive()) {
                return;
            }
            if(server.ShouldStopServer()) {
                return;
            }
        }
    }
}
public class UiAutomatorHttpServer extends NanoHTTPD {

    private final String stop = "/stop";
    private final String ping = "/ping";
    private final String dump = "/dump";

    private UiAutomatorService service;
    private boolean shouldStopServer;


    public UiAutomatorHttpServer(int port) throws RemoteException {
        super(port);
        service = new UiAutomatorService();
        stop = false;
    }

    @Override
    public Response serve(IHTTPSession session) {
        Log.i(Start.Tag, "New request");
        if(session.getUri().equals(ping)) {
            Log.i(Start.Tag, "Sending ping response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
        }

        if(session.getUri().equals(dump)) {
            Log.i(Start.Tag, "Sending dump response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT,  service.dumpWindowHierarchy());
        }

        if(session.getUri().equals(stop)) {
            stopServer();
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
        }

        Log.e(Start.Tag, "Something went wrong!");
        Log.e(Start.Tag, session.getUri());
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
    }

    public void stopServer() {
        Log.i(Start.Tag, "Stopping server");
        shouldStopServer = true;
    }

    public boolean ShouldStopServer() {
        return shouldStopServer;
    }

}
 adb shell am instrument -w -r -e debug false -e port 9030 -e class   com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
我的“服务器”如下所示:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {

    public static final String Tag = "UiDuper";
    private int port;
    private UiAutomatorHttpServer server;

    public Start() {
        Bundle extras = InstrumentationRegistry.getArguments();
        port = 9008;
        if(extras.containsKey("port")) {
            port = Integer.parseInt(extras.getString("port"));
            Log.i(Tag, "UIAutomator server port is " + port);
        }

    }

    @Before
    public void setUp() throws IOException, RemoteException {
        Log.i(Tag, "Start server with port " + port);
        server = new UiAutomatorHttpServer(port);
        server.start();
    }

    @After
    public void TearDown() {
        Log.i(Tag, "Tear down server");
        if(server != null) {
            server.stop();
        }
    }

    @Test
    @LargeTest
    public void RunServer() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            if(server == null)
                return;
            if(!server.isAlive()) {
                return;
            }
            if(server.ShouldStopServer()) {
                return;
            }
        }
    }
}
public class UiAutomatorHttpServer extends NanoHTTPD {

    private final String stop = "/stop";
    private final String ping = "/ping";
    private final String dump = "/dump";

    private UiAutomatorService service;
    private boolean shouldStopServer;


    public UiAutomatorHttpServer(int port) throws RemoteException {
        super(port);
        service = new UiAutomatorService();
        stop = false;
    }

    @Override
    public Response serve(IHTTPSession session) {
        Log.i(Start.Tag, "New request");
        if(session.getUri().equals(ping)) {
            Log.i(Start.Tag, "Sending ping response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
        }

        if(session.getUri().equals(dump)) {
            Log.i(Start.Tag, "Sending dump response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT,  service.dumpWindowHierarchy());
        }

        if(session.getUri().equals(stop)) {
            stopServer();
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
        }

        Log.e(Start.Tag, "Something went wrong!");
        Log.e(Start.Tag, session.getUri());
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
    }

    public void stopServer() {
        Log.i(Start.Tag, "Stopping server");
        shouldStopServer = true;
    }

    public boolean ShouldStopServer() {
        return shouldStopServer;
    }

}
 adb shell am instrument -w -r -e debug false -e port 9030 -e class   com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
在设备上安装apk后,我将执行以下操作:

adb forward tcp:9008 tcp:9030
然后我可以像这样启动服务器:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {

    public static final String Tag = "UiDuper";
    private int port;
    private UiAutomatorHttpServer server;

    public Start() {
        Bundle extras = InstrumentationRegistry.getArguments();
        port = 9008;
        if(extras.containsKey("port")) {
            port = Integer.parseInt(extras.getString("port"));
            Log.i(Tag, "UIAutomator server port is " + port);
        }

    }

    @Before
    public void setUp() throws IOException, RemoteException {
        Log.i(Tag, "Start server with port " + port);
        server = new UiAutomatorHttpServer(port);
        server.start();
    }

    @After
    public void TearDown() {
        Log.i(Tag, "Tear down server");
        if(server != null) {
            server.stop();
        }
    }

    @Test
    @LargeTest
    public void RunServer() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            if(server == null)
                return;
            if(!server.isAlive()) {
                return;
            }
            if(server.ShouldStopServer()) {
                return;
            }
        }
    }
}
public class UiAutomatorHttpServer extends NanoHTTPD {

    private final String stop = "/stop";
    private final String ping = "/ping";
    private final String dump = "/dump";

    private UiAutomatorService service;
    private boolean shouldStopServer;


    public UiAutomatorHttpServer(int port) throws RemoteException {
        super(port);
        service = new UiAutomatorService();
        stop = false;
    }

    @Override
    public Response serve(IHTTPSession session) {
        Log.i(Start.Tag, "New request");
        if(session.getUri().equals(ping)) {
            Log.i(Start.Tag, "Sending ping response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
        }

        if(session.getUri().equals(dump)) {
            Log.i(Start.Tag, "Sending dump response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT,  service.dumpWindowHierarchy());
        }

        if(session.getUri().equals(stop)) {
            stopServer();
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
        }

        Log.e(Start.Tag, "Something went wrong!");
        Log.e(Start.Tag, session.getUri());
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
    }

    public void stopServer() {
        Log.i(Start.Tag, "Stopping server");
        shouldStopServer = true;
    }

    public boolean ShouldStopServer() {
        return shouldStopServer;
    }

}
 adb shell am instrument -w -r -e debug false -e port 9030 -e class   com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
然后像这样转储UI:

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {

    public static final String Tag = "UiDuper";
    private int port;
    private UiAutomatorHttpServer server;

    public Start() {
        Bundle extras = InstrumentationRegistry.getArguments();
        port = 9008;
        if(extras.containsKey("port")) {
            port = Integer.parseInt(extras.getString("port"));
            Log.i(Tag, "UIAutomator server port is " + port);
        }

    }

    @Before
    public void setUp() throws IOException, RemoteException {
        Log.i(Tag, "Start server with port " + port);
        server = new UiAutomatorHttpServer(port);
        server.start();
    }

    @After
    public void TearDown() {
        Log.i(Tag, "Tear down server");
        if(server != null) {
            server.stop();
        }
    }

    @Test
    @LargeTest
    public void RunServer() throws InterruptedException {
        while(true) {
            Thread.sleep(100);
            if(server == null)
                return;
            if(!server.isAlive()) {
                return;
            }
            if(server.ShouldStopServer()) {
                return;
            }
        }
    }
}
public class UiAutomatorHttpServer extends NanoHTTPD {

    private final String stop = "/stop";
    private final String ping = "/ping";
    private final String dump = "/dump";

    private UiAutomatorService service;
    private boolean shouldStopServer;


    public UiAutomatorHttpServer(int port) throws RemoteException {
        super(port);
        service = new UiAutomatorService();
        stop = false;
    }

    @Override
    public Response serve(IHTTPSession session) {
        Log.i(Start.Tag, "New request");
        if(session.getUri().equals(ping)) {
            Log.i(Start.Tag, "Sending ping response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
        }

        if(session.getUri().equals(dump)) {
            Log.i(Start.Tag, "Sending dump response");
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT,  service.dumpWindowHierarchy());
        }

        if(session.getUri().equals(stop)) {
            stopServer();
            return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
        }

        Log.e(Start.Tag, "Something went wrong!");
        Log.e(Start.Tag, session.getUri());
        return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
    }

    public void stopServer() {
        Log.i(Start.Tag, "Stopping server");
        shouldStopServer = true;
    }

    public boolean ShouldStopServer() {
        return shouldStopServer;
    }

}
 adb shell am instrument -w -r -e debug false -e port 9030 -e class   com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner

问题是它似乎在一段时间后停止工作,我可以看到以下情况:

  • 停止回应
  • 所有adb命令停止工作(adb logcat、adb shell等)。他们没有给出任何错误,但什么也没有发生
然后,我可以关闭测试/服务器窗口,但它无法解决问题-adb仍不会响应。有时,它会在一段时间后开始响应,但随后服务器将不再工作(我可以启动测试,但我不能ping或转储)

Adb kill/start也将使其再次响应Adb命令,但在那里我无法再次ping服务器

唯一能让它再次发挥作用的是:

  • 插入和拔出usb(大部分时间可以工作)
  • 重新启动设备
重新安装APKs似乎没有多大作用

所以我开始认为我在这里做了一些非常错误的事情,因为这不应该是一件大事(或者你真的很糟糕?)。logcat或uiautomator事件日志也没有说明任何内容

我尝试在服务器循环中添加一些睡眠,但这似乎也没有改变任何事情