• python 获取android 应用使用情况


    python 获取android 应用使用情况

    本文主要讲述python 脚本获取android 应用使用情况。

    主要思路:使用adb 获取当前activity ,1s 一次输出。

    主要涉及知识点:

    1. python 如何执行adb 命令
    2. python 获取当前时间
    3. python 读写文件
    4. python matplotlib的暂停功能

    具体代码实现如下:

    #!/usr/bin/python
    #coding=utf-8
    import matplotlib.pyplot as plt
    import os
    import time
    adb_cmd = "adb shell 'dumpsys activity | grep \"Recent #0\"' > usage_log.txt"
    while True:
        result = os.popen(adb_cmd).read()
        currentTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        with open('usage_log.txt', 'r') as f:
            lines = f.readlines();
        for line in lines:
            with open('usage.txt', 'a') as f:
                f.write(currentTime)
                f.write(line)
        plt.pause(1)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    具体的输出会生成两个文件:

    usage_log.txt:

      * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    
    • 1

    usage.txt:

    2023-11-20 13:39:39  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    2023-11-20 13:39:40  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    2023-11-20 13:39:41  * Recent #0: Task{a730670 #648 visible=true type=standard mode=fullscreen translucent=false A=10328:com.tencent.mm U=0 StackId=648 sz=3}
    2023-11-20 13:39:42  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    2023-11-20 13:39:44  * Recent #0: Task{d6183e9 #646 visible=true type=standard mode=fullscreen translucent=false A=10327:com.ss.android.ugc.aweme U=0 StackId=646 sz=1}
    2023-11-20 13:39:45  * Recent #0: Task{d6183e9 #646 visible=true type=standard mode=fullscreen translucent=false A=10327:com.ss.android.ugc.aweme U=0 StackId=646 sz=1}
    2023-11-20 13:39:46  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    2023-11-20 13:39:47  * Recent #0: Task{1f4ffa2 #649 visible=true type=standard mode=fullscreen translucent=false A=10288:com.zh.xpose U=0 StackId=649 sz=1}
    2023-11-20 13:39:49  * Recent #0: Task{6853b11 #650 visible=true type=standard mode=fullscreen translucent=false A=10287:com.test.dexpoxed U=0 StackId=650 sz=1}
    2023-11-20 13:39:50  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    2023-11-20 13:39:51  * Recent #0: Task{a730670 #648 visible=true type=standard mode=fullscreen translucent=false A=10328:com.tencent.mm U=0 StackId=648 sz=3}
    2023-11-20 13:39:52  * Recent #0: Task{c73bebe #2 visible=false type=home mode=fullscreen translucent=true I=com.oppo.launcher/.Launcher U=0 StackId=1 sz=1}
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    什么是SSL/TLS ?
    Java学习复杂的对象数组操作
    c#常用的数据结构
    石油化工行业商业供应链管理系统:大数据支撑,打造高质量可视化供应链
    【Unity精华一记】特殊文件夹
    一步解决Logcat日志错误:read: unexpected EOF!
    【mysql】出现 slow sql 问题及建议
    Golang Break、Continue跳出多层循环
    【蓝桥杯选拔赛真题21】C++行李运费 第十二届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
    Go语言中结构体struct与字节数组[]byte的相互转换
  • 原文地址:https://blog.csdn.net/qq_23025319/article/details/134505828