gpt4 book ai didi

android - 如何通过python启动android应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 03:54:56 25 4
gpt4 key购买 nike

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

import os,time

for i in range(0,3):

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

最佳答案

是的,您可以使用os.system 来执行ADB 命令。如果您想验证命令是否成功执行,请查看 subprocess 库中的 check_output(...) 函数。这段代码 fragment 是我选择实现 check_output 函数的方式。完整代码请查看 here

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 question 向您详细展示了可用于通过命令行 Intent 启动应用程序的选项。


其他提示:
我创建了一个用 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

关于android - 如何通过python启动android应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19185906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com