Recent

Author Topic: How to detect battery status?  (Read 5602 times)

Marion

  • Full Member
  • ***
  • Posts: 125
How to detect battery status?
« on: April 23, 2012, 10:44:09 pm »
My app needs to be able to tell how much battery life is left and if it is charging. Does anyone have an code sample for this?
Thank you,
Marion
(A recovering Windows programmer.)

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: How to detect battery status?
« Reply #1 on: April 24, 2012, 08:40:45 am »
What system are you using? Your avatar is not clear. It could be interpreted as a "programmer recovering from windows programming"  ;)

If you are still on windows you can use the following code (timer updating label1):
Code: [Select]
uses ...windows;
...
procedure TForm1.Timer1Timer(Sender: TObject);
var
  PowerStatus:TSystemPowerStatus;
begin
  if GetSystemPowerStatus(PowerStatus) then
    begin
    if (PowerStatus.BatteryFlag and 8) <>0 then
      label1.Caption:='Charging'
    else
      label1.Caption:=IntToStr(PowerStatus.BatteryLifePercent) +'% ( '+
        IntToStr(PowerStatus.BatteryLifeTime div 60) +' mins left )';
    end;
end;

Marion

  • Full Member
  • ***
  • Posts: 125
Re: How to detect battery status?
« Reply #2 on: April 24, 2012, 12:10:41 pm »
Thank you for the reply. This is exactly what I need.

As for recovering from windows programming I fell off the wagon. I took a job last week that will be mostly windows. I will get to program for android. After over a year unemployed I am just greatful for a job.
Thank you,
Marion
(A recovering Windows programmer.)

 

TinyPortal © 2005-2018