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)

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

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}

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}

python 获取android 应用使用情况的更多相关文章

  1. 写python获取android设备的GPS及姿态信息

    在android上,我们可以使用QPython来编写.执行Python脚本.它对很多android 系统函数进行了方便的封装,使用QPython编写功能简单的小程序异常方便. 这个示例是我之前用来读取 ...

  2. Python 通过脚本获取Android的apk的部分属性,再通过加密算法生成秘钥。

    Python 通过脚本获取Android的apk的部分属性,再通过加密算法生成秘钥. #!/usr/bin/env python # -*- coding: utf- -*- import os im ...

  3. 收藏的技术文章链接(ubuntu,python,android等)

    我的收藏 他山之石,可以攻玉 转载请注明出处:https://ahangchen.gitbooks.io/windy-afternoon/content/ 开发过程中收藏在Chrome书签栏里的技术文 ...

  4. python获取字母在字母表对应位置的几种方法及性能对比较

    python获取字母在字母表对应位置的几种方法及性能对比较 某些情况下要求我们查出字母在字母表中的顺序,A = 1,B = 2 , C = 3, 以此类推,比如这道题目 https://project ...

  5. 关于Python 获取windows信息收集

    收集一些Python操作windows的代码 (不管是自带的or第三方库)均来自网上 1.shutdown 操作 定时关机.重启.注销 #!/usr/bin/python #-*-coding:utf ...

  6. [python实用代码片段]python获取当前时间的前一天,前一周,前一个月

    python获取当前时间的前一天,前一周,前一个月. 实用python的datetime.timedelta方法,避免了有的月份是30和31等不同的情况. 获取前一个月的时间,方法实现:首先datet ...

  7. Python 获取时间戳

    Python 获取时间通过 time 模块 如下代码,是通过获取当前的时间,按照格式输出 Python默认获取当前的时间返回的都是时间的元组,下面是元组的,字符串时间的一个转换输出 # -*- cod ...

  8. 获取Android设备唯一标识码

    概述 有时需要对用户设备进行标识,所以希望能够得到一个稳定可靠并且唯一的识别码.虽然Android系统中提供了这样设备识别码,但是由于Android系统版本.厂商定制系统中的Bug等限制,稳定性和唯一 ...

  9. uiautomator2 使用Python测试 Android应用

    GitHub地址:https://github.com/openatx/uiautomator2 介绍 uiautomator2 是一个可以使用Python对Android设备进行UI自动化的库.其底 ...

  10. 【Android】让Python在Android系统上飞一会儿

    第一节 在手机上配置Python运行环境 1.下载和安装 Scripting Layer for Android (SL4A) Scripting Layer for Android (SL4A) 是 ...

随机推荐

  1. 华为云ECS上搭建Hadoop集群环境启动时报错“java.net.BindException: Cannot assign requested address”问题的解决

    启动时使用: ./sbin/start-all.sh 1 报错: java.net.BindException: Problem binding to [test7972:9000] java.net ...

  2. 万字长文深度解读Java线程池,硬核源码分析

    前言 本文将深入分析Java线程池的源码,包括线程池的创建.任务提交.工作线程的执行和线程池的关闭等过程.通过对线程池源码的解析,我们能够更好地理解线程池的原理和机制,为我们在实际开发中合理使用线程池 ...

  3. 6.1 KMP算法搜索机器码

    KMP算法是一种高效的字符串匹配算法,它的核心思想是利用已经匹配成功的子串前缀的信息,避免重复匹配,从而达到提高匹配效率的目的.KMP算法的核心是构建模式串的前缀数组Next,Next数组的意义是:当 ...

  4. Solution -「CF 392C」Yet Another Number Sequence

    Description Link. 求 \(\sum_{i=1}^{n}\text{fibonacci}_{i}\times i^{k}=\sum_{i=1}^{n}(F_{i-1}+\text{fi ...

  5. destoon关于archiver归档的性能优化

    今天在处理一个项目时候发现archiver单个模块归档超过百万数据,打开速度就特慢,所以打开archiver下index.php文件进行分析,发现有句sql作怪 1 $result = $db-> ...

  6. tensorboard可视化点云

    tensorboard可视化点云 用 tensorboard 自带的 add_mesh 方法:支持可视化点云和网格,参考链接: tensorflow 网站的tensorboard pytorch 网站 ...

  7. Django框架项目之项目基础——项目介绍、需求分析、pip安装源、环境搭建、前台、后台、跨域请求

    文章目录 1 路飞学城 1.1 企业的web项目类型 1.2 企业项目开发流程 1.3 立项申请阶段 2. 需求分析 2.1 首页 2.2 登录注册 2.3 课程列表 2.4 课程详情 2.5 购物车 ...

  8. 使用 Helm 管理应用的一些 Tips

    背景 Helm 是一个 Kubernetes 的包管理工具,有点类似于 Mac 上的 brew,Python 中的 PIP:可以很方便的帮我们直接在 kubernetes 中安装某个应用. 比如我们可 ...

  9. Production Environment Difference Between Development, Stage, And Production

    There are three different environments that you'll probably deal with at some point. Each environmen ...

  10. .NET的各种对象在内存中如何布局[博文汇总]

    在过去一段时间里,我陆陆续续写一些关于.NET对象类型布局的文章,其中包括值类型和引用类型的内存布局.字符串对象和数组的内存布局等,这里作一个简单的汇总. [1] 如何计算一个实例占用多少内存? 我们 ...