在这里我可以看到手机电池可以工作多长时间?我能以某种方式在我的应用程序中获取这些信息吗?
这是获取电池健康温度的代码,但我不需要这个。我只需要时间,如下图所示:
在这里我可以看到手机电池可以工作多长时间?我能以某种方式在我的应用程序中获取这些信息吗?
这是获取电池健康温度的代码,但我不需要这个。我只需要时间,如下图所示:
I don't think you can get this value directly from the battery info. What you can do however is this.
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 againAnd 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.
我猜你可以使用BatterStatusImpl
和IInterface
调用IBatteryStats
这些是来自 android git 的私有 api 分叉