Delphi XE6のAndroidアプリケーションでバッテリーの情報を取得する

Delphi XE5のAndroidアプリケーションでバッテリーの情報を取得するを少しだけ変更しています。

Androidのバッテリー情報を取得するにはandroid.os.BatteryManagerクラスを使用します。

android.os.BatteryManagerクラスをDelphiに移植します。

専用のユニットを作成します。

unit Android.BatteryManager;

interface

uses Androidapi.JNI.JavaTypes, Androidapi.JNIBridge;

type
  JBatteryManagerClass = interface(JObjectClass)
    ['{739F38F8-E2A2-4016-8739-3F073A0E3E60}'] // CTRL+SHIFT+G
    function _GetBATTERY_HEALTH_COLD: Integer;
    function _GetBATTERY_HEALTH_DEAD: Integer;
    function _GetBATTERY_HEALTH_GOOD: Integer;
    function _GetBATTERY_HEALTH_OVERHEAT: Integer;
    function _GetBATTERY_HEALTH_OVER_VOLTAGE: Integer;
    function _GetBATTERY_HEALTH_UNKNOWN: Integer;
    function _GetBATTERY_HEALTH_UNSPECIFIED_FAILURE: Integer;
    function _GetBATTERY_PLUGGED_AC: Integer;
    function _GetBATTERY_PLUGGED_USB: Integer;
    function _GetBATTERY_PLUGGED_WIRELESS: Integer;
    function _GetBATTERY_STATUS_CHARGING: Integer;
    function _GetBATTERY_STATUS_DISCHARGING: Integer;
    function _GetBATTERY_STATUS_FULL: Integer;
    function _GetBATTERY_STATUS_NOT_CHARGING: Integer;
    function _GetBATTERY_STATUS_UNKNOWN: Integer;
    function _GetEXTRA_HEALTH: JString;
    function _GetEXTRA_ICON_SMALL: JString;
    function _GetEXTRA_LEVEL: JString;
    function _GetEXTRA_PLUGGED: JString;
    function _GetEXTRA_PRESENT: JString;
    function _GetEXTRA_SCALE: JString;
    function _GetEXTRA_STATUS: JString;
    function _GetEXTRA_TECHNOLOGY: JString;
    function _GetEXTRA_TEMPERATURE: JString;
    function _GetEXTRA_VOLTAGE: JString;

    property BATTERY_HEALTH_COLD: Integer read _GetBATTERY_HEALTH_COLD;
    property BATTERY_HEALTH_DEAD: Integer read _GetBATTERY_HEALTH_DEAD;
    property BATTERY_HEALTH_GOOD: Integer read _GetBATTERY_HEALTH_GOOD;
    property BATTERY_HEALTH_OVERHEAT: Integer read _GetBATTERY_HEALTH_OVERHEAT;
    property BATTERY_HEALTH_OVER_VOLTAGE: Integer
      read _GetBATTERY_HEALTH_OVER_VOLTAGE;
    property BATTERY_HEALTH_UNKNOWN: Integer read _GetBATTERY_HEALTH_UNKNOWN;
    property BATTERY_HEALTH_UNSPECIFIED_FAILURE: Integer
      read _GetBATTERY_HEALTH_UNSPECIFIED_FAILURE;
    property BATTERY_PLUGGED_AC: Integer read _GetBATTERY_PLUGGED_AC;
    property BATTERY_PLUGGED_USB: Integer read _GetBATTERY_PLUGGED_USB;
    property BATTERY_PLUGGED_WIRELESS: Integer
      read _GetBATTERY_PLUGGED_WIRELESS;
    property BATTERY_STATUS_CHARGING: Integer read _GetBATTERY_STATUS_CHARGING;
    property BATTERY_STATUS_DISCHARGING: Integer
      read _GetBATTERY_STATUS_DISCHARGING;
    property BATTERY_STATUS_FULL: Integer read _GetBATTERY_STATUS_FULL;
    property BATTERY_STATUS_NOT_CHARGING: Integer
      read _GetBATTERY_STATUS_NOT_CHARGING;
    property BATTERY_STATUS_UNKNOWN: Integer read _GetBATTERY_STATUS_UNKNOWN;
    property EXTRA_HEALTH: JString read _GetEXTRA_HEALTH;
    property EXTRA_HEALTHEXTRA_HEALTHEXTRA_ICON_SMALL: JString
      read _GetEXTRA_ICON_SMALL;
    property EXTRA_LEVEL: JString read _GetEXTRA_LEVEL;
    property EXTRA_PLUGGED: JString read _GetEXTRA_PLUGGED;
    property EXTRA_PRESENT: JString read _GetEXTRA_PRESENT;
    property EXTRA_SCALE: JString read _GetEXTRA_SCALE;
    property EXTRA_STATUS: JString read _GetEXTRA_STATUS;
    property EXTRA_TECHNOLOGY: JString read _GetEXTRA_TECHNOLOGY;
    property EXTRA_TEMPERATURE: JString read _GetEXTRA_TEMPERATURE;
    property EXTRA_VOLTAGE: JString read _GetEXTRA_VOLTAGE;
  end;

  [JavaSignature('android/os/BatteryManager')]
  JBatteryManager = interface(JObject)
    ['{73085A7C-18A7-4295-A0E2-A7B7D2C0EACB}'] // CTRL+SHIFT+G
  end;

  TJBatteryManager = class(TJavaGenericImport<JBatteryManagerClass,
    JBatteryManager>)
  end;

implementation

end.

フォームにボタン(TButton)とメモ(TMemo)を配置します。

001

ボタンのOnClickイベントを追加します。

uses Android.BatteryManager,
  Androidapi.Helpers,
  Androidapi.JNI.GraphicsContentViewText,
  FMX.Platform.Android;


procedure TForm1.Button1Click(Sender: TObject);
var
  Filter: JIntentFilter;
  Battery: JIntent;
  Status, Health, Level, Scale, IconSmall, Plugged, Voltage,
    Temperature: Integer;
  Present: Boolean;
  Technology, StatusString, HealthString, ScString: string;
begin
  Filter := TJIntentFilter.JavaClass.init
    (TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
  Battery := MainActivity.registerReceiver(nil, Filter);

  Status := Battery.getIntExtra(StringToJString('status'), 0);
  Health := Battery.getIntExtra(StringToJString('health'), 0);
  Present := Battery.getBooleanExtra(StringToJString('present'), false);
  Level := Battery.getIntExtra(StringToJString('level'), 0);
  Scale := Battery.getIntExtra(StringToJString('scale'), 0);
  IconSmall := Battery.getIntExtra(StringToJString('icon-small'), 0);
  Plugged := Battery.getIntExtra(StringToJString('plugged'), 0);
  Voltage := Battery.getIntExtra(StringToJString('voltage'), 0);
  Temperature := Battery.getIntExtra(StringToJString('temperature'), 0);
  Technology := JStringToString
    (Battery.getStringExtra(StringToJString('technology')));

  StatusString := '';

  if Status = TJBatteryManager.JavaClass.BATTERY_STATUS_UNKNOWN then
    StatusString := 'UNKNOWN'
  else if Status = TJBatteryManager.JavaClass.BATTERY_STATUS_CHARGING then
    StatusString := 'CHARGING'
  else if Status = TJBatteryManager.JavaClass.BATTERY_STATUS_DISCHARGING then
    StatusString := 'DISCHARGING'
  else if Status = TJBatteryManager.JavaClass.BATTERY_STATUS_NOT_CHARGING then
    StatusString := 'NOT CHARGING'
  else if Status = TJBatteryManager.JavaClass.BATTERY_STATUS_FULL then
    StatusString := 'FULL';

  HealthString := '';

  if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_UNKNOWN then
    HealthString := 'UNKNOWN'
  else if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_GOOD then
    HealthString := 'GOOD'
  else if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_OVERHEAT then
    HealthString := 'OVERHEAT'
  else if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_DEAD then
    HealthString := 'DEAD'
  else if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_OVER_VOLTAGE then
    HealthString := 'OVER VOLTAGE'
  else if Health = TJBatteryManager.JavaClass.BATTERY_HEALTH_UNSPECIFIED_FAILURE
  then
    HealthString := 'UNSPECIFIED FAILURE';

  ScString := '';

  if Plugged = TJBatteryManager.JavaClass.BATTERY_PLUGGED_AC then
    ScString := 'AC'
  else if Plugged = TJBatteryManager.JavaClass.BATTERY_PLUGGED_USB then
    ScString := 'USB';

  Memo1.Lines.Clear;
  Memo1.Lines.Add(Format('%s=%s', ['Status(充電状態)', StatusString]));
  Memo1.Lines.Add(Format('%s=%s', ['Health(バッテリーの状態)', HealthString]));
  Memo1.Lines.Add(Format('%s=%s', ['Present(バッテリーが存在するかどうか)',
    BoolToStr(Present, True)]));
  Memo1.Lines.Add(Format('%s=%d', ['Level(バッテリー残量)', Level]));
  Memo1.Lines.Add(Format('%s=%d', ['Scale(バッテリー残量の最大値)', Scale]));
  Memo1.Lines.Add(Format('%s=%d', ['Icon Small', IconSmall]));
  Memo1.Lines.Add(Format('%s=%s', ['Plugged(AC or USB)', ScString]));
  Memo1.Lines.Add(Format('%s=%d', ['Voltage(バッテリー電圧)', Voltage]));
  Memo1.Lines.Add(Format('%s=%d', ['Temperature(バッテリー電圧)', Temperature]));
  Memo1.Lines.Add(Format('%s=%s', ['Technology', Technology]));
end;

アプリケーションを実行してボタンを押すと、バッテリーの情報が表示されます。

device-2014-07-23-193803

「Delphi XE6のAndroidアプリケーションでバッテリーの情報を取得する」への2件のフィードバック

  1. バッテリーの情報取得のみでなく、充電のON/OFFなどのコントロールを行うことは可能なのでしょうか

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

This site uses Akismet to reduce spam. Learn how your comment data is processed.