ningantai 1 Posted September 24 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
DelphiUdIT 266 Posted September 24 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
Dave Nottage 644 Posted September 24 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
ningantai 1 Posted September 24 Thanks to Dave Nottage, The code you provided works perfectly. 1 Share this post Link to post
alejandro.sawers 19 Posted September 25 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. 1 Share this post Link to post