Get battery charge percentage
To get the percentage of battery charge in an android device, we can use the following code:
BatteryManager bm=(BatteryManager)getSystemService(BATTERY_SERVICE);
int battery_percent = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
The result battery_percent is an integer.
If we have a TextView textview1 on our page, to display the battery percentage in textview1 we can use following code:
textview1.setText(battery_percent + "%");
To check if battery status is full or not, following code can be used:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
if(status == BatteryManager.BATTERY_STATUS_FULL) {
Toast.makeText(getApplicationContext(),"Device is fully charged",Toast.LENGTH_LONG).show(); } else {
Toast.makeText(getApplicationContext(),"Device is not fully charged",Toast.LENGTH_LONG).show(); }
Apart from BATTERY_STATUS_FULL, other battery status integers which can be used include
BATTERY_STATUS_CHARGING
BATTERY_STATUS_DISCHARGING
BATTERY_STATUS_FULL
BATTERY_STATUS_NOT_CHARGING
BATTERY_STATUS_UNKNOWN
The codes can be used whenever you want to show the battery percentage. To show it as soon as app starts, put the code in onCreate event. To show it on button click, put the code in Button onClick event, etc.
To get the battery charging mode, use following code:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
if(usbCharge){
Toast.makeText(getApplicationContext(),"Device charging through USB",Toast.LENGTH_LONG).show();
} else if(acCharge) {
Toast.makeText(getApplicationContext(),"Device charging through AC",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Device not charging",Toast.LENGTH_LONG).show();
}
This code can be used to find out if the device is charging or not.
To get the battery temperature use following code:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int temp = batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
float tempTwo = ((float) temp) / 10;
textview1.setText(tempTwo + " °C");
Thanks!
ReplyDeleteIf you want to set a number or a string to the percentage of the battery you can do this:
ReplyDelete[variable name] = battery_percetage
Put this in the same add source directly as the other code but under it
How
ReplyDeleteNice Post
ReplyDeleteYUVA SWAROZGAR SCHEME
not clear to me...plz exactly write it.
ReplyDelete5 errors
ReplyDeleteNice blog . I have also been making tutorials related to Sketchware. In my Sketchware Interface video I have explaines each and every part of the Sketchware app itself. Do check my channel to learn more about sketchware.
ReplyDeleteMy channel link - https://youtube.com/channel/UCOUL_rPb3vQZoQ_ENC9lB3w
Thanks Sanjeev sir. I have learnt about sketchware by watching your videos . So Thanks a lot.
I guess I am the only one who comes here to share my very own experience guess what? I am using my laptop for almost the post 2 years.
ReplyDeleteBattery Health Crack
AVG PC TuneUp Crack
Home Plan Pro Crack
ReplyDeleteI am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me
Bluetooth Battery Monitor Crack
I guess I am the only one who comes here to share my very own experience guess what? I am using my laptop for almost the post 2 years.
ReplyDeleteBluetooth Battery Monitor Crack
PDFMate PDF Converter Pro Crack
ESET Internet Security Crack
ChrisPC Screen Recorder Pro Crack
Freemake Video Converter Crack
Revo Uninstaller Pro Crack
Microsoft Office Crack
Driver Navigator Crack
Hi, its khan here. I have lot of websites that provide many useful apps free of cost.
ReplyDeleteautodesk revit 2022 crack
getvstcracks
crackinfree
crackwon
crackpromac
crackmakers
crackinmac
Amazing blog! I really like the way you explained such information about this post with us. And blog is really helpful for us this website
ReplyDeletebluetooth-battery-monitor-crack
xnview Crack
ReplyDeleteMalwarebytes Crack
Protonvpn Crack
Ableton Live Suite Crack
imyfone Crack
Filezilla Crack
Vmware Workstation Crack
sketch Crack
teracopy Crack
PDF Expert Crack
Great work with the hard work you have done I appreciate your work thanks for sharing it...
ReplyDeleteTweakBit Anti-Malware Crack
Many thanks for sharing such incredible knowledge. It's really good for your website.
ReplyDeleteThe info on your website inspires me greatly. This website I'm bookmarked. Maintain it and thanks again.
I'm really impressed with your writing skills, as smart as the structure of your weblog.
PDFMate PDF Converter Pro Crack
Tutorial Coding if you interesting can visit here
ReplyDelete
ReplyDeleteSo nice I am enjoying for that post as for u latest version of this Security tool Available
bluetooth-battery-monitor-keygen download
Nice article and explanation Keep continuing to write an article like this you may also check my website Crack Softwares Download We established Allywebsite in order to Create Long-Term Relationships with Our Community & Inspire Happiness and Positivity we been around since 2015 helping our people get more knowledge in building website so welcome aboard the ship.
ReplyDeletehttps://idmcracksetup.com/revo-uninstaller-pro-crack-plus-license-key/
https://idmcracksetup.com/minitool-partition-wizard-pro-crack/
https://idmcracksetup.com/eset-endpoint-security-crack/
I thought this was a pretty interesting read when it comes to this topic. Thank you
ReplyDeletePDFMate PDF Converter Pro Crack
Large Software PC Tune-Up Pro Crack
IVT Bluesoleil Crack
Nuclear Coffee VideoGet Crack
Helicon Focus Pro Crack
up4crack.net