0

在这里我可以看到手机电池可以工作多长时间?我能以某种方式在我的应用程序中获取这些信息吗?

这是获取电池健康温度的代码,但我不需要这个。我只需要时间,如下图所示:

在此处输入图像描述

4

3 回答 3

5

I don't think you can get this value directly from the battery info. What you can do however is this.

  1. Read percentage remaining (e.g. x%) and current timestamp
  2. Register a broadcast receiver for battery level changes - and in onReceive read the new percentage (e.g. y%, usually it would be x - 1, but it may be x - 2 or whatever) and get the timestamp again
  3. Get the difference between the two timestamps and the two percentages (x - y) - this will show how much time the (x - y)% of battery lasts. Now just extrapolate that to y% - and you have your answer

And you can use the code in the question that you linked to determine the percentage.

For example, you get 80% battery at timestamp 1361882535654. Then when you get 79%, your timestamp is 1361882801234. This means that 1% of battery lasts about 265 seconds, hence the remaining time on the battery is approximately 79 * 265 = 209305 seconds, which is 2 days 10 hours 8 minutes and 25 seconds.

Yes, this naturally takes some time (i.e. you have to wait those 200 or whatever seconds for the battery power to go down). The reason your system app can display it immediately is because the system process runs in the background continuously and most likely does that as it goes along.

于 2013-02-26T12:47:36.810 回答
2

我猜你可以使用BatterStatusImplIInterface调用IBatteryStats这些是来自 android git 的私有 api 分叉

于 2013-05-22T06:17:02.483 回答
1

看看Intent.ACTION_POWER_USAGE_SUMMARY

于 2013-02-26T12:40:30.117 回答