[转载] python通过adb获取android手机耗电量

参考链接: 从Python中控制台获取输入

把开发者模式打开,激活 adb 调试,然后可以使用以下python代码获取安卓手机的耗电量 

# -*- coding: utf-8 -*-

 

import re

import os

 

def getSelectDevice():

    pip = os.popen('adb devices')

    result = pip.read()

    devices_split = result.split('\n')

    devices = []

    for device in devices_split:

        if device == '':

            continue

        devices.append(device)

       

    if len(devices) < 2:

        print('设备不存在')

        return -1

    

    if len(devices) == 2: # 只有一台设备,直接返回

        return devices[1].split('\t')[0]

    

    print("选择你要操作的设备")

    for index in range(1,len(devices)):

        print("%s:\t%s" % (index,devices[index]))

 

 

    print("输入编号:")

    select=int(input())

    selectline = devices[select]

    return selectline.split('\t')[0]

    

def getBatteryInfo(device):

    pip = os.popen('adb -s %s shell dumpsys batterystats' % device)

    result = pip.buffer.read().decode(encoding='utf8')

    return result

    

def parsePowerInfo(info):

    

    start = 0

    

    try:

        start = info.index('Estimated power use')

    except Exception:

        print('耗电量信息不存在')

        return -1

    if start < 0:

        print('耗电量信息不存在')

        return -1

    start = info.index('Capacity',start)

    end = info.index('\n',start)

        

    result = info[start:end]

    

    capacity = re.findall('\d+\.?\d*', result)

    

    return capacity

 

def parseResetTimeInfo(info):

    

    start = 0

    

    try:

        start = info.index('RESET:TIME:')

    except Exception:

        print('重置时间不存在')

        return -1

    if start < 0:

        print('重置时间不存在')

        return -1

    end = info.index('\n',start)

        

    result = info[start:end]

    

    capacity = re.findall('\d+\.?\d*', result)

    return capacity

    

 

 

 

def main():

    device = getSelectDevice()

    if device == -1: 

        return

    print("正在获取信息...")

    betteryinfo = getBatteryInfo(device)

    if betteryinfo == -1: 

        return

    print("正在解析信息...")

    result = parsePowerInfo(betteryinfo)

    if result == -1: 

        return

    

    print("得出结果:")

    print("\t电池容量:%s mA" % (result[0]))

    print("\t计算耗电:%s mA" % (result[1]))

    print("\t实际耗电:%s mA" % (result[2]))

    

    timeinfo = parseResetTimeInfo(betteryinfo)

    if timeinfo == -1:

        return

    print("重置时间:%s-%s-%s %s:%s:%s" % (timeinfo[0],timeinfo[1],timeinfo[2],timeinfo[3],timeinfo[4],timeinfo[5]))

    pass

 

if __name__ == '__main__':

    main()

    pass

        

 

我这边接入了两个设备的控制台输出: 

选择你要操作的设备

1:      192.168.0.103:5555      device

2:      192.168.0.101:5555      device

输入编号:

 

2

正在获取信息...

正在解析信息...

得出结果:

        电池容量:3300 mA

        计算耗电:282 mA

        实际耗电:396 mA

重置时间:2020-07-20 11:22:37

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540623.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ES6之主要知识点(二) 变量的解构赋值。默认值

引自http://es6.ruanyifeng.com/#docs/destructuring 数组解构赋值默认值对象解构赋值用途1.数组的解构赋值 let [a, b, c] [1, 2, 3]; let [foo, [[bar], baz]] [1, [[2], 3]]; foo // 1 bar // 2 baz // 3let [ , , third] ["foo", "bar", "baz&…

python无符号转有符号_Python | 散布符号

python无符号转有符号There are multiple types of Scatter Symbols available in the matplotlib package and can be accessed through the command marker. In this article, we will show some examples of different marker types and also present a list containing all…

[转载] 基于LSTM的股票预测模型_python实现_超详细

参考链接&#xff1a; 从Python获取输入 文章目录 一、背景二、主要技术介绍1、RNN模型2、LSTM模型3、控制门工作原理四、代码实现五、案例分析六、参数设置七、结论完整程序下载 一、背景 近年来&#xff0c;股票预测还处于一个很热门的阶段&#xff0c;因为股票市场的波动…

shell -eom_EOM的完整形式是什么?

shell -eomEOM&#xff1a;消息结尾 (EOM: End Of Message) EOM is an abbreviation of "End Of Message". EOM是“消息结尾”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also written as Eom or eom. It is written at …

在eclipse中启动Tomcat访问localhost:8080失败项目添加进Tomcat在webapp中找不到

软件环境&#xff1a;Eclipse oxygen&#xff0c; Tomcat8.5 #在eclipse中启动Tomcat访问localhost:8080失败 在eclipse中配置tomcat后&#xff0c;打开tomcat后访问localhost:8080后无法出现登陆成功的界面,即无法出现下面的界面 在eclipse中的servers状态栏中双击tomcat&…

[转载] 【基础教程】Python input()函数:获取用户输入的字符串

参考链接&#xff1a; 从Python中控制台获取输入 input() 是 Python 的内置函数&#xff0c;用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容&#xff0c;所以用户输入的内容可以包含任何字符。 input() 函数的用法为&#xff1a; str…

程序员简历工作模式_简历的完整形式是什么?

程序员简历工作模式简历&#xff1a;简历 (CV: Curriculum Vitae) The CV is an abbreviation of Curriculum Vitae. It is a written outline summary of a persons educational training and qualifications and his other experiences. It is an absolute profile of a cand…

[转载] Python新手写出漂亮的爬虫代码1——从html获取信息

参考链接&#xff1a; Python中从用户获取多个输入 Python新手写出漂亮的爬虫代码1 初到大数据学习圈子的同学可能对爬虫都有所耳闻&#xff0c;会觉得是一个高大上的东西&#xff0c;仿佛九阳神功和乾坤大挪移一样&#xff0c;和别人说“老子会爬虫”&#xff0c;就感觉特别…

在Scala中设置&()方法

Scala中的Set&#xff06;()方法 (The Set &() method in Scala) The &() method in the Set is used to create a new set in Scala. This new set created contains all elements from the other two sets that are common for both of the given sets i.e. new set …

[转载] python与c/c++相比有哪些优势

参考链接&#xff1a; Python输入和C, Java速度对比 理论上&#xff0c;python的确比C/C慢&#xff08;我对Java的开发没有经验&#xff0c;无法评论&#xff09;。这一点不用质疑。 C/C是编绎语言&#xff0c;直接使用的是机器指令&#xff0c;而python总是跑在的虚拟机上&am…

清空日志的三种方法

方法一&#xff1a;echo "" >test.log方法二&#xff1a;> test.log方法三&#xff1a;cat /dev/null >test.log转载于:https://www.cnblogs.com/liang545621/p/7528509.html

splat net_Ruby中的Splat参数

splat netRuby Splat参数 (Ruby Splat Arguments) We have learnt how to work with methods in Ruby? We are very well aware of the fact that methods may or may not consume any arguments. Let us discuss the methods which consume argument or have a predefined ar…

ajax的访问 WebService 的方法

转自原文 ajax的访问 WebService 的方法 如果想用ajax进行访问 首先在web.config里进行设置 添加在 <webServices> <protocols> <add name "HttpPost" /> <add name "HttpGet" /> </protocols> </webServices> <s…

[转载] 使用DirectInput进行交互

参考链接&#xff1a; input()函数中的漏洞– Python2.x 使用DirectInput进行交互&#xff08;1&#xff09; DirectX 2008-08-10 15:11:34 阅读169 评论0 字号&#xff1a;大 中 小 订阅 输入设备简介 计算机通常使用三种输入设备&#xff1a;键盘、鼠标和游…

c语言 nan 常量_NaN32常量(Julia)

c语言 nan 常量Julia| NaN32常数 (Julia | NaN32 Constant) NaN32 is a constant of the Float32 type in Julia programming language, it represents "not-a-number" value. NaN32是Julia编程语言中Float32类型的常量&#xff0c;它表示“非数字”值。 Syntax: 句…

Hyperledger Fabric 1.0 从零开始(七)——启动Fabric多节点集群

5&#xff1a;启动Fabric多节点集群 5.1、启动orderer节点服务 上述操作完成后&#xff0c;此时各节点的compose配置文件及证书验证目录都已经准备完成&#xff0c;可以开始尝试启动多机Fabric集群。 首先启动orderer节点&#xff0c;切换至orderer.example.com服务器&#xff…

[转载] python中print()函数的用法和end=““不换行详解

参考链接&#xff1a; Python | print()中的结束参数 需求&#xff1a;打印五个字符&#xff0c;在一行上 代码&#xff1a; i 0 while i< 5 : i 1 print(i,end’’) 结果&#xff1a; 1 2 3 4 5那么问题来了&#xff0c;为什么加一个end"" 就不换…

css中图片左右边距_CSS中的边距

css中图片左右边距CSS保证金属性 (CSS margin property) CSS Margins are used to space around any element, for this we use "margin" property in the CSS. CSS边距用于在任何元素之间留出空间&#xff0c;为此&#xff0c;我们在CSS中使用“ margin”属性 。 S…

js 实现网页显示倒计时

用 js 来实现网页显示倒计时效果 1 function checkTime( time ){2 var data new Data(); // 获取现在时间3 var nowData data.getTime(); // 转化成毫秒数4 var time ; // 结束的时间5 var t time - nowData ;6 var HH, mm , ss 0;7 var sta "…

scala方法中的变量_Scala中的变量

scala方法中的变量Scala变量 (Scala variables) A variable is named a reference to a memory location. The location stores the data that is used by the program. 变量被称为对存储位置的引用。 该位置存储程序使用的数据。 Based on the data type of the variable the…