Learn how to use the ADB command in Python

Source: Internet
Author: User

Using the ADB command in Python, you can import an OS module.

1 simple adb order.

such as: Os.system (' adb version ')


2 A somewhat complicated adb order.

such as: Os.system (' adb shell ' dumpsys activity | grep "Mfocusedactivity" "")


3 want to save the contents of the ADB read out.

such as: out = Os.popen (the ' adb shell ' dumpsys activity | grep "mfocusedactivity" "'). Read ()


To sum up: Os.system does not support reading, Os.popen supports reading.


Here's a transfer script to learn:

Import OS
import re


os.system (' adb version ')
os.system (' adb devices ') #os. System is out of read operation not supported
Os.popen (' adb shell ' dumpsys activity | grep "mfocusedactivity" "'). Read () #os. Popen supports read operations
print (out)

#下面的代码是获取当前窗口的component参数
def Getfocusedpackageandactivity (): Pattern

        = Re.compile (r "[A-za-z0-9\.] +/[a-za-z0-9\.] + ") #这里使用了正则表达式, the content of the output is restricted, only the string out like" Com.mediatek.factorymode/com.mediatek.factorymode.factorymode "is displayed
        Os.popen ("ADB shell dumpsys window Windows | Findstr \/| Findstr name= "). Read () #window下使用findstr
        list = Pattern.findall (out)
        component = List[0] #输出列表中的第一条字符串

        Return component
Print (Getfocusedpackageandactivity ())


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.