简体   繁体   English

在多个设备上同时运行方法-Android

[英]Run Method On Multiple Devices at the Same Time - Android

I currently have an application where I push a button on the main activity and it does data collection. 我目前有一个应用程序,可以在主要活动上按一个按钮,它可以进行数据收集。 I need to do this on multiple devices at the same time. 我需要在多个设备上同时执行此操作。 At the moment, i'm simply trying to do this manually by timing the button press (pushing my fingers down on each device at the same time). 目前,我只是尝试通过定时按一下按钮(同时将手指按在每个设备上)来手动执行此操作。 Obviously this doesn't work practically with more than 2 devices. 显然,这实际上不适用于2个以上的设备。

I'm wondering if there is a way for me to run the method (that is called when the button is pressed) on the devices at the same time from my PC? 我想知道是否可以通过PC在设备上同时运行该方法(在按下按钮时调用该方法)?

I've seen a lot of answers use Appium for this kind of implementation, however, this seems a little too much for what i need. 我已经看到很多答案将Appium用于这种实现,但是,对于我所需要的来说,这似乎有点过多。 Is there not a way to run this via the command line (adp)? 没有办法通过命令行(adp)运行它吗? Or even just set up a small server that the devices connect to where the method is called? 甚至只是建立一台小型服务器,将设备连接到该方法所在的位置?

Thank you. 谢谢。

您可以使用adb通过命令行单击按钮,但需要具有x和y坐标。

adb shell input tap x y

I added code to run the required method when a key was pressed. 我添加了代码,以在按下某个键时运行所需的方法。 In the example code below i used F12. 在下面的示例代码中,我使用了F12。

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    // KEYCODE_F12 = 142
    if (keyCode == KeyEvent.KEYCODE_F12) {
        findViewById(R.id.button).performClick();
    }

    return super.onKeyDown(keyCode, event);
}

The keycode number for F12 is 142. F12的密钥代码号是142。

adb devices is used to get the device ids that are connected. adb devices用于获取连接的设备ID。 I could then use adb to input the key event into the devices: 然后,我可以使用adb将按键事件输入到设备中:

start adb -s device_id1 shell input keyevent 142
start adb -s device_id2 shell input keyevent 142

The 'start' command is used to try and run the adb commands in parallel as much as possible to reduce the input lag to the devices. “开始”命令用于尝试并并行运行adb命令,以减少设备的输入延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM