在python中使用adb sendevent

时间:2013-08-23 16:17:45

标签: android python adb

我遇到了一个奇怪的问题,从命令行运行adb shell sendevent x x x命令工作正常,但是当我使用以下任何一项时:

subprocess.Popen(['adb', 'shell', 'sendevent', 'x', 'x','x'])
 subprocess.Popen('adb shell sendevent x x x', shell=True)
 subprocess.call(['adb', 'shell', 'sendevent', 'x', 'x','x'])

它们都失败了 - 即使在shell脚本中工作的模拟触摸在通过python调用时也无法正常工作。此外,我尝试adb push shell脚本到设备,并使用adb shell /system/sh /sdcard/script.sh我能够成功运行它,但当我尝试通过python运行该命令行时,脚本失败。

更奇怪的是,他的脚本运行,但是,例如,它似乎没有在脚本的一半执行命令sleep 1echo命令工作,sendevent命令似乎没有用。

甚至看起来不可能,但确实如此。如何通过python运行一组adb shell sendevent x x x命令?

1 个答案:

答案 0 :(得分:0)

  • sendevent需要4个参数
  • 对于args
  • Popen应为['adb', 'shell', 'sendevent /dev/input/eventX type code value'] - 不要拆分远程命令
  • 时序对于sendevent序列非常重要,而adb shell调用本身也很昂贵 - 因此在设备上使用shell脚本效果会更好
  • 注意shell脚本中的换行符 - 确保它是unix样式(单\n而不是\r\n