电池的寿命
有没有办法或如何获得或知道电池的寿命?我正在制作电池信息,就像在“Battery Doctor Saver Pro”中一样,区别在于我需要的使用寿命。我不知道该怎么做。谁能帮我解决这个问题吗?感谢您的帮助。 :) 到目前为止,这是我的代码,没有我所说的“生命周期”。
private BroadcastReceiver battery_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isPresent = intent.getBooleanExtra("present", false);
String technology = intent.getStringExtra("technology");
int plugged = intent.getIntExtra("plugged", -1);
int scale = intent.getIntExtra("scale", -1);
int health = intent.getIntExtra("health", 0);
int status = intent.getIntExtra("status", 0);
int rawlevel = intent.getIntExtra("level", -1);
int level = 0;
Bundle bundle = intent.getExtras();
Log.i("BatteryLevel", bundle.toString());
if (isPresent) {
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
String info = "Battery Level: " + level + "%\n";
info += ("Technology: " + technology + "\n");
info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
info += ("Health: " + getHealthString(health) + "\n");
info += ("Status: " + getStatusString(status) + "\n");
setBatteryLevelText(info + "\n\n" + bundle.toString());
} else {
setBatteryLevelText("Battery not present!!!");
}
}
};
private String getPlugTypeString(int plugged) {
String plugType = "Unknown";
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
plugType = "AC";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
plugType = "USB";
break;
}
return plugType;
}
private String getHealthString(int health) {
String healthString = "Unknown";
switch (health) {
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "Dead";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "Good Condition";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "Over Voltage";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "Over Heat";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "Failure";
break;
}
return healthString;
}
private String getStatusString(int status) {
String statusString = "Unknown";
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "Charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "Discharging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "Full";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "Not Charging";
break;
}
return statusString;
}
is there a way or how to get or to know the life span of the battery? I'm making a Battery Information just like in the "Battery Doctor Saver Pro" the difference is that the life span that i need. i don't have any idea on how to do it. can anyone help me with this? THANKS for your help. :) so far this is my code without the "Life Span" that i said.
private BroadcastReceiver battery_receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isPresent = intent.getBooleanExtra("present", false);
String technology = intent.getStringExtra("technology");
int plugged = intent.getIntExtra("plugged", -1);
int scale = intent.getIntExtra("scale", -1);
int health = intent.getIntExtra("health", 0);
int status = intent.getIntExtra("status", 0);
int rawlevel = intent.getIntExtra("level", -1);
int level = 0;
Bundle bundle = intent.getExtras();
Log.i("BatteryLevel", bundle.toString());
if (isPresent) {
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
String info = "Battery Level: " + level + "%\n";
info += ("Technology: " + technology + "\n");
info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
info += ("Health: " + getHealthString(health) + "\n");
info += ("Status: " + getStatusString(status) + "\n");
setBatteryLevelText(info + "\n\n" + bundle.toString());
} else {
setBatteryLevelText("Battery not present!!!");
}
}
};
private String getPlugTypeString(int plugged) {
String plugType = "Unknown";
switch (plugged) {
case BatteryManager.BATTERY_PLUGGED_AC:
plugType = "AC";
break;
case BatteryManager.BATTERY_PLUGGED_USB:
plugType = "USB";
break;
}
return plugType;
}
private String getHealthString(int health) {
String healthString = "Unknown";
switch (health) {
case BatteryManager.BATTERY_HEALTH_DEAD:
healthString = "Dead";
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
healthString = "Good Condition";
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
healthString = "Over Voltage";
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
healthString = "Over Heat";
break;
case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
healthString = "Failure";
break;
}
return healthString;
}
private String getStatusString(int status) {
String statusString = "Unknown";
switch (status) {
case BatteryManager.BATTERY_STATUS_CHARGING:
statusString = "Charging";
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
statusString = "Discharging";
break;
case BatteryManager.BATTERY_STATUS_FULL:
statusString = "Full";
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
statusString = "Not Charging";
break;
}
return statusString;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不太确定您在这里要做什么,要计算瓦特小时或安培小时,请检查此链接:
http://www.powerstream.com/battery-capacity-calculations.htm
如果您想要当前使用的电池的剩余时间,您需要获取当前的电池电量,然后了解其耗尽的速度,方法是检查在此之前耗尽的速度,然后做出假设或通过知道您将消耗多少电流。
如果您针对您正在使用的确切电池校准代码,将会获得最佳结果
(看起来我被打败了;))
Not exactly sure what you are trying to do here, to calculate watt hours or amp hours check this Link:
http://www.powerstream.com/battery-capacity-calculations.htm
If you want the time left on the battery you are currently using you need to get the current battery level and then know how fast it is being drained, either by checking how fast it has been drained up until then and then making an assumption or by knowing how much current you will draw.
Best results will be achieved if you calibrate your code for the exact battery you are using
(looks like i got beaten to the answer ;) )
正如我在获取 Android 手机的剩余电池使用时间中所说,您需要获取当前的电池电量并进行工作对当前排水速度进行一些估计。这将取决于处理器负载、屏幕亮度、哪些无线电处于活动状态以及可能的其他因素。
简短的回答,当然可以得到一个估计,但这并不容易,而且可能不是很准确
As I said on Get the remaining battery time available from an Android phone, you will need to get the current battery level and work out some estimate for how fast it is currently being drained. This will depend on processor load, screen brightness, what radios are active and probably a load of other factors.
Short answer, it's certainly possible to get an estimate but it won't be easy and may not be very accurate