Firemonkey Android Gets Battery Level

I have a function:

function BatteryPercent(const aContext: JContext): Integer;
var
  filter: JIntentFilter;
  battery: JIntent;
  level, scale: Integer;
begin
  filter := TJIntentFilter.Create;
  filter.addAction(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);

  battery := aContext.registerReceiver(NIL, filter);
  level := battery.getIntExtra(StringToJString('level'), -1);
  scale := battery.getIntExtra(StringToJString('scale'), -1);

  result := (100 * level) div scale;
end;

But what should I pass as aContextparam?

I need to charge the battery every minute and keep it in the memo ...

+4
source share
1 answer

You can read this article on your blog; " Battery Information (Android - XE5) ." The original article is written in Spanish, but you can try automatic translation (to the right of the page).

You can read the code and download the sample project (with sources).

The code I use is similar to:

  // Contexto
  myContext := SharedActivityContext;

  // Creamos y Configuramos el Intent
  filter := TJIntentFilter.Create;
  // Asociamos la ACTION que queremos capturar
  filter.addAction(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
  // lo registramos
  intentBatt := myContext.registerReceiver(nil, filter);

enter image description here

Sincerely.

+2
source

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


All Articles