问题
The BatteryManager class doesn't have the metric for current: http://developer.android.com/reference/android/os/BatteryManager.html
And then I have found this post: Getting the battery current values for the Android Phone It seems that the author also found it was not possible to get such value from the Linux entry.
I also downloaded the widget called CurrentWidget into my Nexus 7. For the current value, it shows "no data".
So probably a current sensor is required to get the current value and some android systems just do not have such sensors. Then how can the android system know the current battery level? Maybe it will just use the voltage of the battery to infer that(voltage will decrease during the discharge)? But then it would be very coarse-grained. Someone says that there is some 'trick' used to estimate the battery level without current values in Android(probably based on Voltage?). Is there any reference?
PS: for some reason, I know current value can be recorded on iphone 3GS
回答1:
try this code,may be it would be help full for you:
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
//this will give you battery current status
int level = intent.getIntExtra("level", 0);
contentTxt.setText(String.valueOf(level) + "%");
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
textView2.setText("status:"+status);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
textView3.setText("is Charging:"+isCharging);
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
textView4.setText("is Charge plug:"+chargePlug);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
textView5.setText("USB Charging:"+usbCharge+" AC charging:"+acCharge);
}
};
in main class register this using:
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
来源:https://stackoverflow.com/questions/16765926/in-android-is-there-a-way-to-get-the-battery-current