我想在重启平板电脑后运行任何应用程序(比如设置)。我可以使用os.system
还是必须使用其他方法。
import os,time
for i in range(0,3):
os.system("adb reboot")
time.sleep(60)
答案 0 :(得分:2)
是的,您可以使用os.system
执行ADB命令。如果要验证成功执行的命令,请查看check_output(...)
库之外的check_output
函数。此代码snipet是我选择实现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)
函数的方式。完整代码外观subprocess。
am start -n yourpackagename/.activityname
要启动应用程序,您可以使用命令adb shell am start -n com.android.settings/com.android.settings.Settings
。要启动“设置应用”,请运行time.sleep(60)
。此stackoverflow here向您详细显示了可用于通过命令行intent启动应用程序的选项。
其他提示:
我创建了一个用python编写的ADB包装器以及一些其他python实用程序,这些实用程序可能有助于您尝试完成的任务。例如,不是调用sys.boot_completed
等待重新启动,而是使用adb来轮询属性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
的状态,一旦设置了属性,设备就完成了启动,您可以启动任何应用程序。以下是您可以使用的参考实现。
{{1}}