On Android, is there a way to get battery current?

The BatteryManager class does not have a metric for current: http://developer.android.com/reference/android/os/BatteryManager.html

And so I found this post: Getting current battery values ​​for an Android phone It seems that the author also found that it was not possible to get such a value from a Linux record.

I also downloaded a widget called CurrentWidget into my Nexus 7. For the current value, it shows "no data".

Therefore, it is likely that a current sensor is required to obtain the current value, and in some Android systems there are no such sensors. Then how can the Android system know the current battery level? Maybe he will just use the battery voltage to conclude that (the voltage will decrease during discharge)? But then it would be very rude. Someone says that there is some β€œtrick” used to estimate the battery level without current values ​​in Android (probably based on voltage?). Is there a link?

PS: for some reason I know that the current value can be recorded on iphone 3GS

+4
source share
1 answer

try this code, maybe this will help you fully:

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 the main class, register this using:

  this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
+1
source

Source: https://habr.com/ru/post/1482880/


All Articles