Jump to content
Sign in to follow this  
ningantai

How to get the battery capacity of an Android phone?

Recommended Posts

Following are Java's Reflection APIs to retreive the value returned by getBatteryCapacity method of com.android.internal.os.PowerProfile :

Can anyone convert this to Delphi 12 syntax?

public double getBatteryCapacity(Context context) {
    Object mPowerProfile;
    double batteryCapacity = 0;
    final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

    try {
        mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context.class)
                .newInstance(context);

        batteryCapacity = (double) Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getBatteryCapacity")
                .invoke(mPowerProfile);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return batteryCapacity;

}

Share this post


Link to post

In a very very old project I did this:

procedure TForm5.Button1Click(Sender: TObject);
 var
  ifl: JIntentFilter;
  ctx: JContext;
  bStatus: JIntent;
  level, scale: Integer;
 begin
  //(get the battery status intent) to determine the current state of charge
  ifl := TJintentFilter.JavaClass.init(TJIntent.JavaClass.ACTION_BATTERY_CHANGED);
  ctx := TAndroidHelper.Context;
  bStatus := ctx.registerReceiver(nil, ifl);


  //string reality is that the level of BatteryManager.EXTRA_LEVEL
  //  reality of//BatteryManager.EXTRA_SCALE string that Scale
  level := bStatus.getIntExtra(StringToJString('level'), -1);
  scale := bStatus.getIntExtra(StringToJstring('scale'), -1);

  //calculate the % state of charge
  ListBox1.Items.Add(IntToStr((100*level) div scale));
end;

 

Share this post


Link to post
2 hours ago, ningantai said:

Can anyone convert this to Delphi 12 syntax?

The JNI Bridge can be used (as it can for other Java classes, like in many other Java imports in Delphi) for this: 

uses
  Androidapi.JNIBridge, Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers;

type
  JPowerProfile = interface;

  JPowerProfileClass = interface(JObjectClass)
    ['{A0EDE09D-94EB-447F-AEF8-C0761A5C4BFA}']
    {class} function init(context: JContext): JPowerProfile; cdecl;
  end;

  [JavaSignature('com/android/internal/os/PowerProfile')]
  JPowerProfile = interface(JObject)
    ['{AADA92E9-6D6C-438B-A997-24637F9155E6}']
    function getBatteryCapacity: Double; cdecl;
  end;
  TJPowerProfile = class(TJavaGenericImport<JPowerProfileClass, JPowerProfile>) end;

function GetBatteryCapacity: Double;
begin
  Result := TJPowerProfile.JavaClass.init(TAndroidHelper.Context).getBatteryCapacity;
end;

Not sure how useful it's going to be. My device is reporting 18% and the value this function returned was 5003. The source for PowerProfile is here - perhaps someone can make sense of it 🙂

 

Share this post


Link to post
22 hours ago, Dave Nottage said:

the value this function returned was 5003

I think it's the typical (i.e. as listed in the device specs) capacity expressed in mAh.

Also, note that PowerProfile is greylisted since Android 9. You can use it now but it could stop working on a future Android version, so keep an eye on that.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×