如何通过 Python 在设备上启动 Android 应用程序?
How can I launch an Android app on a device through Python?
我已经查阅了有关该主题的多个主题,但我没有看到任何与直接使用 ppadb
命令在设备上启动应用程序相关的信息。
我设法完成了这段代码:
import ppadb
import subprocess
from ppadb.client import Client as AdbClient
# Create the connect functiun
def connect():
client = AdbClient(host='localhost', port=5037)
devices = client.devices()
for device in devices:
print (device.serial)
if len(devices) == 0:
print('no device connected')
quit()
phone = devices[0]
print (f'connected to {phone.serial}')
return phone, client
if __name__ == '__main__':
phone, client = connect()
import time
time.sleep(5)
# How to print each app on the emulator
list = phone.list_packages()
for truc in list:
print(truc)
# Launch the desired app through phone.shell using the package name
phone.shell(????????????????)
从那里,我可以访问每个应用程序包 (com.package.name)。我想通过 phone.shell()
命令启动它,但我无法访问正确的语法。
我可以执行点击或按键事件,而且效果很好,但我想确保我的代码不会因位置的任何变化而受到干扰。
从 How to start an application using Android ADB tools 开始,启动应用程序的 shell 命令是
am start -n com.package.name/com.package.name.ActivityName
因此你会调用
phone.shell("am start -n com.package.name/com.package.name.ActivityName")
一个给定的包可能有多个活动。要找出它们是什么,您可以使用 dumpsys package
,如下所示:
def parse_activities(package, connection, retval):
out = ""
while True:
data = connection.read(1024)
if not data: break
out += data.decode('utf-8')
retval.clear()
retval += [l.split()[-1] for l in out.splitlines() if package in l and "Activity" in l]
connection.close()
activities = []
phone.shell("dumpsys package", handler=lambda c: parse_activities("com.package.name", c, activities))
print(activities)
这是正确且最简单的答案:
phone.shell('monkey -p com.package.name 1')
此方法将启动应用程序而无需访问 ActivityName
使用 AndroidViewClient/cluebra,您可以启动包的 MAIN
Activity,如下所示:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from com.dtmilano.android.viewclient import ViewClient
ViewClient.connectToDeviceOrExit()[0].startActivity(package='com.example.package')
这将连接到设备(必要时等待),然后仅使用包名称调用startActivity()
。
startActivity()
也可以收到一个 component
,当你知道包和 activity.
时使用
我已经查阅了有关该主题的多个主题,但我没有看到任何与直接使用 ppadb
命令在设备上启动应用程序相关的信息。
我设法完成了这段代码:
import ppadb
import subprocess
from ppadb.client import Client as AdbClient
# Create the connect functiun
def connect():
client = AdbClient(host='localhost', port=5037)
devices = client.devices()
for device in devices:
print (device.serial)
if len(devices) == 0:
print('no device connected')
quit()
phone = devices[0]
print (f'connected to {phone.serial}')
return phone, client
if __name__ == '__main__':
phone, client = connect()
import time
time.sleep(5)
# How to print each app on the emulator
list = phone.list_packages()
for truc in list:
print(truc)
# Launch the desired app through phone.shell using the package name
phone.shell(????????????????)
从那里,我可以访问每个应用程序包 (com.package.name)。我想通过 phone.shell()
命令启动它,但我无法访问正确的语法。
我可以执行点击或按键事件,而且效果很好,但我想确保我的代码不会因位置的任何变化而受到干扰。
从 How to start an application using Android ADB tools 开始,启动应用程序的 shell 命令是
am start -n com.package.name/com.package.name.ActivityName
因此你会调用
phone.shell("am start -n com.package.name/com.package.name.ActivityName")
一个给定的包可能有多个活动。要找出它们是什么,您可以使用 dumpsys package
,如下所示:
def parse_activities(package, connection, retval):
out = ""
while True:
data = connection.read(1024)
if not data: break
out += data.decode('utf-8')
retval.clear()
retval += [l.split()[-1] for l in out.splitlines() if package in l and "Activity" in l]
connection.close()
activities = []
phone.shell("dumpsys package", handler=lambda c: parse_activities("com.package.name", c, activities))
print(activities)
这是正确且最简单的答案:
phone.shell('monkey -p com.package.name 1')
此方法将启动应用程序而无需访问 ActivityName
使用 AndroidViewClient/cluebra,您可以启动包的 MAIN
Activity,如下所示:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
from com.dtmilano.android.viewclient import ViewClient
ViewClient.connectToDeviceOrExit()[0].startActivity(package='com.example.package')
这将连接到设备(必要时等待),然后仅使用包名称调用startActivity()
。
startActivity()
也可以收到一个 component
,当你知道包和 activity.