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

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

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

今回は、Delphi XE6のAndroidアプリケーションでバッテリーの情報を取得するで作成したAndroid.BatteryManagerユニットを使用します。

Android.BatteryManager.pasを作成して、プロジェクトに追加します。

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.

プロジェクトをビルドすると、Android.BatteryManager.hppが作成されます。

#include "Android.BatteryManager.hpp"

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

001

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

#include "Android.BatteryManager.hpp"
#include <Androidapi.Helpers.hpp>
#include <Androidapi.JNI.GraphicsContentViewText.hpp>
#include <FMX.Platform.Android.hpp>


void __fastcall TForm2::Button1Click(TObject *Sender)
{
  _di_JIntentFilter Filter = TJIntentFilter::JavaClass->init(TJIntent::JavaClass->ACTION_BATTERY_CHANGED);
  _di_JIntent Battery = MainActivity()->registerReceiver(NULL, Filter);

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

  UnicodeString StatusString = "";

  if (Status == TJBatteryManager::JavaClass->BATTERY_STATUS_UNKNOWN) StatusString = "UNKNOWN";
  else if (Status == TJBatteryManager::JavaClass->BATTERY_STATUS_CHARGING) StatusString = "CHARGING";
  else if (Status == TJBatteryManager::JavaClass->BATTERY_STATUS_DISCHARGING) StatusString = "DISCHARGING";
  else if (Status == TJBatteryManager::JavaClass->BATTERY_STATUS_NOT_CHARGING) StatusString = "NOT CHARGING";
  else if (Status == TJBatteryManager::JavaClass->BATTERY_STATUS_FULL) StatusString = "FULL";

  UnicodeString HealthString = "";

  if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_UNKNOWN) HealthString = "UNKNOWN";
  else if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_GOOD) HealthString = "GOOD";
  else if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_OVERHEAT) HealthString = "OVERHEAT";
  else if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_DEAD) HealthString = "DEAD";
  else if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_OVER_VOLTAGE) HealthString = "OVER VOLTAGE";
  else if (Health == TJBatteryManager::JavaClass->BATTERY_HEALTH_UNSPECIFIED_FAILURE) HealthString = "UNSPECIFIED FAILURE";

  UnicodeString ScString = "";

  if (Plugged == TJBatteryManager::JavaClass->BATTERY_PLUGGED_AC) ScString = "AC";
  else if (Plugged == TJBatteryManager::JavaClass->BATTERY_PLUGGED_USB) ScString = "USB";

  Memo1->Lines->Clear;
  Memo1->Lines->Add(u"Status(充電状態)="+ StatusString);
  Memo1->Lines->Add(u"Health(バッテリーの状態)=" + HealthString);
  Memo1->Lines->Add(u"Present(バッテリーが存在するかどうか)=" + BoolToStr(Present, True));
  Memo1->Lines->Add(u"Level(バッテリー残量)=" + IntToStr(Level));
  Memo1->Lines->Add(u"Scale(バッテリー残量の最大値)=" + IntToStr(Scale));
  Memo1->Lines->Add(u"Icon Small=" + IntToStr(IconSmall));
  Memo1->Lines->Add(u"Plugged(AC or USB)" + ScString);
  Memo1->Lines->Add(u"Voltage(バッテリー電圧)=" + IntToStr(Voltage));
  Memo1->Lines->Add(u"Temperature(バッテリー電圧)=" + IntToStr(Temperature));
  Memo1->Lines->Add(u"Technology==" + Technology);
}

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

device-2014-07-23-200747

コメントを残す

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

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください