获取电量
adb shell dumpsys battery
获取电池状态
adb shell dumpsys battery set status 1 (2 为可充 其余为不可充电)
import os
import time
import csv
class Controller():
def __init__(self,count):
self.count = count
self.alldata = [("timestamp","battery")]
def testprocess(self):
result = os.popen("adb shell dumpsys battery")
for line in result:
if "level" in line:
power = line.split(":")[1]
currenttime = self.getCurrentTime()
self.alldata.append((currenttime,power))
def run(self):
os.popen("adb shell dumpsys battery set status 1")
while self.count > 0:
self.testprocess()
self.count = count -1
time.sleep(5)
def getCurrentTime(self):
time = time.strtime("%Y-%m-%d %H:%M:%S",time.localtime())
return time
def SaveDataToCsv(self):
csvfile = file("power.csv","wb")
writercsv = csv.writer(csvfile)
writer = writercsv.writerows(self.alldata)
csvfile.close()