Mindee

主要记录各种包的使用代码例子, 遇到问题的处理方式 项目记录更新全部代码

  博客园  :: 首页  :: 新随笔  ::  ::  :: 管理

ADB

  1. 获取序列号:

    adb get-serialno
  2. 查看连接计算机的设备:

    adb devices
  3. 重启机器:

    adb reboot
  4. 重启到bootloader,即刷机模式:

    adb reboot bootloader
  5. 重启到recovery,即恢复模式:

    adb reboot recovery
  6. 查看log:

    adb logcat
  7. 终止adb服务进程:

    adb kill-server
  8. 重启adb服务进程:

    adb start-server 
  9. 获取机器MAC地址:

    adb shell  cat /sys/class/net/wlan0/address
  10. 获取CPU序列号:

    adb shell cat /proc/cpuinfo
  11. 安装APK:

    adb install <apkfile> //比如:adb install baidu.apk
  12. 保留数据和缓存文件,重新安装apk:

    adb install -r <apkfile> //比如:adb install -r baidu.apk
  13. 安装apk到sd卡:

    adb install -s <apkfile> // 比如:adb install -s baidu.apk
  14. 卸载APK:

    adb uninstall <package> //比如:adb uninstall com.baidu.search
  15. 卸载app但保留数据和缓存文件:

    adb uninstall -k <package> //比如:adb uninstall -k com.baidu.search
  16. 启动应用:

    adb shell am start -n <package_name>/.<activity_class_name> 
  17. 查看设备cpu和内存占用情况:

    adb shell top
  18. 查看占用内存前6的app:

    adb shell top -m 6
  19. 刷新一次内存信息,然后返回:

    adb shell top -n 1
  20. 查询各进程内存使用情况:

    adb shell procrank
  21. 杀死一个进程:

    adb shell kill [pid] 
  22. 查看进程列表:

    adb shell ps
  23. 查看指定进程状态:

    adb shell ps -x [PID] 
  24. 查看后台services信息:

    adb shell service list 
  25. 查看当前内存占用:

    adb shell cat /proc/meminfo
  26. 查看IO内存分区:

    adb shell cat /proc/iomem
  27. 将system分区重新挂载为可读写分区:

    adb remount
  28. 从本地复制文件到设备:

    adb push <local> <remote> 
  29. 从设备复制文件到本地:

    adb pull <remote> <local> 
  30. 列出目录下的文件和文件夹,等同于dos中的dir命令:

    adb shell ls
  31. 进入文件夹,等同于dos中的cd 命令:

    adb shell cd <folder> 
  32. 重命名文件:

    adb shell rename path/oldfilename path/newfilename 
  33. 删除system/avi.apk:

    adb shell rm /system/avi.apk
  34. 删除文件夹及其下面所有文件:

    adb shell rm -r <folder> 
  35. 移动文件:

    adb shell mv path/file newpath/file
  36. 设置文件权限:

    adb shell chmod 777 /system/fonts/DroidSansFallback.ttf
  37. 新建文件夹:

    adb shell mkdir path/foldelname
  38. 查看文件内容:

    adb shell cat <file> 
  39. 查看wifi密码:

    adb shell cat /data/misc/wifi/*.conf 
  40. 清除log缓存:

    adb logcat -c
  41. 查看bug报告:

    adb bugreport
  42. 获取设备名称:

    adb shell cat /system/build.prop
  43. 查看ADB帮助:

    adb help
  44. 跑monkey:

    adb shell monkey -v -p your.package.name 500 

 

https://blog.csdn.net/xxdw1992/article/details/120673011

Adb shell dumpsys –l 查看dumpsys的列表

Adb shell dumpsys :默认答应出当前系统所有service信息,在后面可以具体加上服务名

Adb shell dumpsys | findstr DUMP :需要列出当前运行的服务,可运行

Adb shell dumpsys SurfaceFlinger :获取桌面显示数据

Adb shell dumpsys battery :获取设备电池信息

Adb shell dumpsys display :获取设备分辨率

Adb shell dumpsys cpuinfo :获取设备CPU信息

Adb shell dumpsys meminfo :获取内存信息 , 要获取具体应用的内存信息,可以加上包名如adb shell dumpsys PACKAGE_NAME

查看手机某个应用版本命令如:adb shell dumpsys package com.tencent.mobileqq |grep version

查看当前手机焦点的activity方法:Linux通过adb shell dumpsys activity | grep “mResume”(windows下grep改成findstr)

Adb shell dumpsys activity | find “mF”

Adb shell dumpsys activity | grep –I run

adb shell dumpsys window |grep mCurrent

获取Acticity信息:adb shell dumpsys activity ,加上-h可以获取帮助信息

获取当前界面的UI信息,可以用:adb shell dumpsys activity top | findstr 类名

查看当前应用包名和类名:adb shell dumpsys window | findstr 关键词

获取package信息:adb shell dumpsys package 加上-h可以获取帮助信息

获取某个包的信息:adb shell dumpsys package 包名

获取通知信息:adb shell dumpsys notification

位置信息:adb shell dumpsys location

获取电源管理信息:adb shell dumpsys power

可以获取到是否处于锁屏状态:mWakefulness=Asleep或者mScreeenOn=false

 

亮度值:mScreenBrightness=255

屏幕休眠时间:Screen off timeout : 60000ms

屏幕分辨率:

mDisplayWidth=1440,mDisplayHeight=2560

获取电话状态信息:adb shell dumpsys telephony.registry 例如:

mCallstate值为0,表示待机状态,1表示来电未接听状态,2表示电话占线状态

mCallForwarding=false#是否启用呼叫转移

mDataConnectionState=2 #0:无数据连接1:正在创建数据连接2:已连接

mDateConnectionPossible=true #是否有数据连接

获取某个指定的类:adb shell dumpsys activity | findstr 类名

查找带有“comp.hl”的配置项属性值:adb shell getprop | find “comp.hl”

getprop[key]取得对应的key的属性值

getprop列出所有配置属性值如果有修改属性的话,很简单,只需要修改字典值就可以了,如:

setprop[key][value]设置指定key的属性值;

watchprops监听系统属性的变化,如果期间系统的属性发生变化则把变化的值显示出来

其实这三个命令都是toolbox的子命令,如果有兴趣的可以看在android源码中看对应的源码:system/core/toolbox/

CameraService注册的服务名称为“media.camera”通过执行adb shell dumpsys media.camera >camerainfo.txt可以将进程信息导出到文件中

dumpsys activity "bk.yt.example"

easy-dumpsys 可以看到app活动页面的层级资料

 

解析获取apk包的信息

from androguard.core.bytecodes.apk import APK

apk_file = "path/to/apk/file"
a = APK(apk_file)

package_name = a.get_package()
print(package_name)

from androguard.core.bytecodes.apk import APK

apk_file = "path/to/apk/file"
a = APK(apk_file)

activities = a.get_activities()
for activity in activities:
    print(activity)

 

自动解析并点击文件

import uiautomator2 as u2
import time

# connect to the device
device = u2.connect()

# extract device model
model = device.info["brand"] + " " + device.info["model"]

# extract package name and main activity of the app
package = "com.example.myapp"
activity = device.app_start(package)

# wait for the app to start
time.sleep(5)

# simulate a touch event to select an option
device(text="Option 1").click()

 

安卓自动化

import uiautomator2 as u2
import time

# connect to the device
device = u2.connect()

# extract device model
model = device.info["brand"] + " " + device.info["model"]

# extract package name and main activity of the app
package = "com.example.myapp"
activity = device.app_start(package)

# wait for the app to start
time.sleep(5)

# simulate a touch event to select an option
device(text="Option 1").click()

 

定位下拉框

import uiautomator2 as u2

# Connect to the device
d = u2.connect()

# Find the dropdown menu element
dropdown = d(className="android.widget.Spinner").info

# Get the number of options in the dropdown
num_options = len(dropdown["children"])

# Scroll the dropdown until the desired option is visible
desired_option = "Option 3"
for i in range(num_options):
    # Get the text of the current option
    option_text = d(className="android.widget.Spinner").child(className="android.widget.TextView")[i].info["text"]
    
    if option_text == desired_option:
        # If the desired option is already visible, click on it
        d(className="android.widget.Spinner").child(className="android.widget.TextView")[i].click()
        break
        
    # If the desired option is not visible, scroll the dropdown
    if i == num_options - 1:
        # We have reached the end of the dropdown and the desired option is still not visible
        raise ValueError(f"Option '{desired_option}' not found in dropdown")

    d.swipe(dropdown["bounds"]["left"], dropdown["bounds"]["top"], dropdown["bounds"]["left"], dropdown["bounds"]["bottom"])

 

 Linux兼容, ssh连接的包

import paramiko
import uiautomator2 as u2

# Connect to the Android device
d = u2.connect()

# Start an SSH session with the Linux device
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.100', username='username', password='password')

# Run a command on the Linux device
stdin, stdout, stderr = ssh.exec_command('ls -l')
print(stdout.read())

# Run a command on the Android device
d.app_start('com.example.myapp')
d(text='OK').click()

# Close the SSH session
ssh.close()

 

Linux 启动程序

from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QProcess

# Replace "application_name" with the name of the application you want to start
application_name = "firefox"

# Create a QGuiApplication object (required for starting GUI applications)
app = QGuiApplication([])

# Create a QProcess object and start the application
process = QProcess()
process.startDetached(application_name)

from PyQt5.QtGui import QGuiApplication
from PyQt5.QtCore import QProcess

# Create a QGuiApplication object (required for starting GUI applications)
app = QGuiApplication([])

# Start Firefox with a specific URL
process = QProcess()
process.startDetached("firefox", ["https://www.example.com"])

 

 

 

The uiautomator2 library is a Python wrapper around the uiautomator framework, which provides an API for automating interactions with Android devices.

Here are some of the other functions provided by the uiautomator2 library:

  • device.info: Get information about the connected device, such as its model, serial number, Android version, and battery level.

  • device.press(keycode): Send a key event to the device, such as a key press or a key release. You can use the keycode constants provided by the uiautomator2 library, such as uiautomator2.KeyCode.HOME, to specify the key event to send.

  • device(text="text").click(): Find an element on the screen with the specified text, and click on it.

  • device(resourceId="id").click(): Find an element on the screen with the specified resource ID, and click on it.

  • device(className="class").click(): Find an element on the screen with the specified class name, and click on it.

  • device.swipe(start_x, start_y, end_x, end_y): Perform a swipe gesture on the screen from the specified starting point to the specified ending point.

  • device(scrollable=True).scroll(steps=100): Scroll the screen in the direction specified by the steps argument. If scrollable=True, the scrollable element on the screen will be scrolled. If scrollable=False, the entire screen will be scrolled.

  • device.orientation: Get or set the orientation of the device.

  • device.takeScreenshot(): Take a screenshot of the device's screen and save it as a PIL image.

  • device.wait(condition, timeout=10): Wait for a condition to be true, such as an element to be visible on the screen or a specific text to be displayed.

  • device.xpath("xpath_expression"): Find an element on the screen that matches the specified XPath expression, and return a UiObject that represents it.

These are just a few examples of the functions provided by the uiautomator2 library. You can find more information and examples in the uiautomator2 documentation.

 

posted on 2022-12-07 01:08  Mindee  阅读(97)  评论(0)    收藏  举报