1.反射出类com.android.internal.os.PowerProfile得到类对象PowerProfile
2.使用PowerProfile的getAveragePower分别得到wifi单位耗电值wifiCost,数据流量单位耗电值radioCost,gps单位耗电值gpsCost
3.在android 4.1以上系统解析系统文件/proc/net/xt_qtaguid/iface_stat_all分别得到数据流量值radioBytes和wifi流量值wifiBytes
使用公式radioPower = radioBytes*radioCost得到数据流量耗电量radioPower
4.在android 4.1以下系统上解析系统文件/proc/net/xt_qtaguid/stats分别得到数据流量值radioBytes和wifi流量值wifiBytes
使用公式wifiPower = wifiBytes*wifiCost得到wifi耗电量wifiPower
5.
1).使用PowerProfile的getNumCpuClusters得到cpu的簇数cpuClusterNum
2).获取到当前进程的pid,解析proc/pid/stat得到当前进程的cpu的utime,stime.cutime,cstime
3).使用公式cpuTime = utime + stime + cutime +cstime得到总的cpu使用时长cpuTime
4).遍历每个cpu簇,使用PowerProfile的getNumSpeedStepsInCpuCluster得到每个簇的步频数,并累加得到总的cpu步频数totalCpuSteps
5).假定每个步频下的运行时长是一样的,则使用公式
averageStepTime = cpuTime/totalCpuSteps
得到每个步频下的cpu耗电值averageStepTime
6)遍历所有步频,
1).使用PowerProfile的getAveragePowerForCpu得到某个cpu簇下某个步频的cpu单位耗
电值cpuClusterStepCost
2).使用公式
cpuClusterStepPower = cpuClusterStepCost*averageStepTime
得到当前簇当前步频的cpu耗电量cpuClusterStepPower
3).累加所有cpuClusterStepPower得到总的cpu耗电量totalCpuPower
6.
1).使用aop面向切面编程分别切入以android系统api requestLocationUpdates 和removeUpdates,在程序运行时,分别得到requestLocationUpdates的调用时间点time1,和removeUpdates的调用时间点time2,
2).使用公式:gpsTime = time2 – time1, 得到gps使用时长gpsTime
3).使用公式:gpsPower = gpsCost * gpsTime
7.使用公式:
totalPower = radioPower + wifiPower + totalCpuPower,得到当前应用总的耗电量totalPower