2

我想在重新启动平板电脑后运行任何应用程序(比如设置)。我可以使用os.system还是必须使用其他方法。

import os,time

for i in range(0,3):

    os.system("adb reboot")
    time.sleep(60) 
4

1 回答 1

2

是的,您可以使用os.system来执行 ADB 命令。如果要验证成功执行的命令,请查看子进程check_output(...)中的函数。此代码片段是我选择实现该功能的方式。完整代码请看这里check_output

def _run_command(self, cmd):
"""
Execute an adb command via the subprocess module. If the process exits with
a exit status of zero, the output is encapsulated into a ADBCommandResult and
returned. Otherwise, an ADBExecutionError is thrown.
"""
try:
    output = check_output(cmd, stderr=subprocess.STDOUT)
    return ADBCommandResult(0,output)
except CalledProcessError as e:
    raise ADBProcessError(e.cmd, e.returncode, e.output)


要启动应用程序,您可以使用命令am start -n yourpackagename/.activityname。要启动设置应用程序,请运行adb shell am start -n com.android.settings/com.android.settings.Settings. 这个 stackoverflow问题向您详细展示了可用于通过命令行意图启动应用程序的选项。


其他提示:
我创建了一个用 python 编写的 ADB 包装器以及一些其他 python 实用程序,它们可能有助于您尝试完成的工作。例如,不是调用time.sleep(60)等待重启,而是使用 adb 来轮询属性的状态,sys.boot_completed一旦设置了属性,设备就完成了启动,您可以启动任何应用程序。以下是您可以使用的参考实现。

def wait_boot_complete(self, encryption='off'):
"""
When data at rest encryption is turned on, there needs to be a waiting period 
during boot up for the user to enter the DAR password. This function will wait
till the password has been entered and the phone has finished booting up.

OR

Wait for the BOOT_COMPLETED intent to be broadcast by check the system 
property 'sys.boot_completed'. A ADBProcessError is thrown if there is an 
error communicating with the device. 

This method assumes the phone will eventually reach the boot completed state.

A check is needed to see if the output length is zero because the property
is not initialized with a 0 value. It is created once the intent is broadcast.

"""
if encryption is 'on':
  decrypted = None
  target = 'trigger_restart_framework'
  print 'waiting for framework restart'
  while decrypted is None:
    status = self.adb.adb_shell(self.serial, "getprop vold.decrypt")
    if status.output.strip() == 'trigger_restart_framework':
      decrypted = 'true'

  #Wait for boot to complete. The boot completed intent is broadcast before
  #boot is actually completed when encryption is enabled. So 'key' off the 
  #animation.
  status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
  print 'wait for animation to start'
  while status == 'stopped':
    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()

  status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()
  print 'waiting for animation to finish'
  while status == 'running':
    status = self.adb.adb_shell(self.serial, "getprop init.svc.bootanim").output.strip()        

else:
  boot = False
  while(not boot):      
    self.adb.adb_wait_for_device(self.serial)
    res = self.adb.adb_shell(self.serial, "getprop sys.boot_completed")
    if len(res.output.strip()) != 0 and int(res.output.strip()) is 1:
      boot = True
于 2013-10-04T18:43:19.370 回答