The task is very common, but I can not find implementation on QT 5.x.

We need to know the current battery level of the smartphone / tablet on Android.

  • @helpforprogrammer, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

Here's the usual java method, maybe it will help:

public float getBatteryLevel() { Intent batteryIntent = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if(level == -1 || scale == -1) { return 50.0f; } return ((float)level / (float)scale) * 100.0f; } 
  • Thank. On Java works. Now I try to connect this method to QT via JNI. If you know how to connect them, tell me. - helpforprogrammer
  • How to use this code in QT5 JNI? I get the error: java.lang.RuntimeException: Can't create a handler inside thread that has not called Looper.prepare () - helpforprogrammer