DelphiFan Magazine

Top Menu

Main Menu

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS

logo

DelphiFan Magazine

  • DELPHI
  • CODE SAMPLES
  • FIREMONKEY
  • DATABASE
  • RELEASES
  • VIDEOS
  • REVIEW
  • TECH NEWS
  • How to add a server application written in UNIGUI to startup?

  • UniGUI Expiry Link Anonymizer

  • UniGUI Add CAPTCHA to the WEB project

  • UniGui. Unique hybrid of Delphi and JS

  • How to transform a blob field into string lines (varchar) in SQL Firebird?

CODE SAMPLESDELPHIFIREMONKEY
Home›CODE SAMPLES›Delphi recognizes the battery level of an Android device

Delphi recognizes the battery level of an Android device

By admin
December 10, 2020
210
0
Share:

You may want to know the battery level.
On Android, the battery manager broadcasts intent information about the battery.

To find out the battery level, get the battery level and battery level and calculate the percentage.
The code is based on the Android developer training article “Monitoring battery power and charging status“.

Sample code

Put buttons and list boxes on the form

Just press a button to detect power

Write the detected% value into the list box

Describe the process. (Confirmed through 10.2 Tokyo, Nexus 5 and Android 5.1.1)

unit Unit1;
interface
uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ListBox,
  Androidapi.Helpers, Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes
  ;
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
{private declaration}
  public
  {public declaration}
  end;
var
  Form1: TForm1;
implementation
{$R *.fmx}
procedure TForm1.Button1Click(Sender: TObject);
var
  ifl: JIntentFilter;
  ctx: JContext;
  bStatus: JIntent;
  level, scale: Integer;
begin
//Determine the current state of charge (get battery state intent)
  ifl := TJintentFilter.JavaClass.init(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
  ctx := TAndroidHelper.Context;
  bStatus := ctx.registerReceiver(nil, ifl);
//BatteryManager.EXTRA_LEVEL string//Realistic level // Realistic string scale of BatteryManager.EXTRA_SCALE
  level := bStatus.getIntExtra(StringToJString(‘level’), -1);
  scale := bStatus.getIntExtra(StringToJstring(‘scale’), -1);
  ListBox1.Items.Add(‘Battery’);
//Calculated state of charge%
  ListBox1.Items.Add(IntToStr((100*level) div scale));
end;
end.
Tagsandroidandroid batteryandroid battery leveldelphidelphi android
Previous Article

Delphi changes Android brightness

Next Article

Delphi Android internal system related operations

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0
  • 0

admin

Related articles More from author

  • CODE SAMPLESDELPHI

    Customize the height of uniGUI Grid header

    October 1, 2020
    By admin
  • CODE SAMPLESDELPHI

    How to set up Unigui to run on Linux Operating System

    October 11, 2020
    By admin
  • DELPHIRELEASES

    DevExpress VCL Controls 20.1.5 released

    September 10, 2020
    By admin
  • CODE SAMPLESDELPHI

    UniGUI special objects

    November 11, 2020
    By admin
  • DELPHIVIDEOS

    Per Form VCL Styling

    September 29, 2020
    By admin
  • CODE SAMPLESDELPHI

    Check Out 30+ Delphi Units To Take Your Development

    October 8, 2020
    By admin

Leave a reply Cancel reply

You may interested

  • CODE SAMPLESDELPHI

    UniGUI special objects

  • CODE SAMPLESDELPHI

    Introduction to the properties of the delphi TForm class

  • DELPHIRELEASES

    SVGICONIMAGELIST 2.2: New SVG engine “CAIRO”

  • LATEST REVIEWS

  • TOP REVIEWS

Timeline

  • January 27, 2021

    How to add a server application written in UNIGUI to startup?

  • January 27, 2021

    UniGUI Expiry Link Anonymizer

  • January 26, 2021

    UniGUI Add CAPTCHA to the WEB project

  • January 26, 2021

    UniGui. Unique hybrid of Delphi and JS

  • January 13, 2021

    How to transform a blob field into string lines (varchar) in SQL Firebird?

Latest Comments

Find us on Facebook

Follow Us on Instagram

logo

Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.

About us

  • 660 Pennsylvania Avenue Southeast #100 Washington, DC 20003
  • 0123456789
  • adsdelphi@gmail.com
  • Recent

  • Popular

  • Comments

  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • UniGUI Expiry Link Anonymizer

    By admin
    January 27, 2021
  • UniGUI Add CAPTCHA to the WEB project

    By admin
    January 26, 2021
  • UniGui. Unique hybrid of Delphi and JS

    By admin
    January 26, 2021
  • How to add a server application written in UNIGUI to startup?

    By admin
    January 27, 2021
  • Amazing Cross Platform Email App Sample In Delphi 10.4.1 FireMonkey For Android And IOS

    By admin
    August 13, 2020
  • Critical Update Available For Delphi FireMonkey 10.4.1 On Android, IOS, OSX, Windows, And Linux

    By admin
    September 4, 2020
  • Setting up the IDE for your first Android Application in Delphi

    By admin
    September 7, 2020

Follow us

Find us on Facebook

© Copyright Delphifan Forum. All rights reserved.