Qt6 Gettting Android Battery Data.
-
I have a java file (QtBattery.java)
import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; public class QtBattery { public static double getBatteryPercentage(Context context) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); if (batteryStatus != null) { int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if (level != -1 && scale != -1) { return ((level * 100.0)/ scale); } } return -1.0; // Return -1.0f if battery status cannot be retrieved } }
A c++ method in a QWidget class that periodically is called to check the Android battery status.
#ifdef ANDROID jdouble QtTest4::batteryStatus() { jdouble batteryLevel=-1.0; QJniObject activity = QNativeInterface::QAndroidApplication::context(); if (activity.isValid()) { QJniObject context = activity.callObjectMethod("getApplicationContext", "()Landroid/content/Context;"); if (context.isValid()) { batteryLevel = context.callStaticMethod<jdouble>("com/home/QtTest4/QtBattery", "getBatteryPercentage"); } } return batteryLevel; } #endif
The AndroidManifest.xml includes:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
The problem is the batteryLevel variable is always returned as zero (0.0).
I am looking for suggestions or ideas has to what is wrong.
Thanks. -
I have a java file (QtBattery.java)
import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; public class QtBattery { public static double getBatteryPercentage(Context context) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); if (batteryStatus != null) { int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); if (level != -1 && scale != -1) { return ((level * 100.0)/ scale); } } return -1.0; // Return -1.0f if battery status cannot be retrieved } }
A c++ method in a QWidget class that periodically is called to check the Android battery status.
#ifdef ANDROID jdouble QtTest4::batteryStatus() { jdouble batteryLevel=-1.0; QJniObject activity = QNativeInterface::QAndroidApplication::context(); if (activity.isValid()) { QJniObject context = activity.callObjectMethod("getApplicationContext", "()Landroid/content/Context;"); if (context.isValid()) { batteryLevel = context.callStaticMethod<jdouble>("com/home/QtTest4/QtBattery", "getBatteryPercentage"); } } return batteryLevel; } #endif
The AndroidManifest.xml includes:
<uses-permission android:name="android.permission.BATTERY_STATS"/>
The problem is the batteryLevel variable is always returned as zero (0.0).
I am looking for suggestions or ideas has to what is wrong.
Thanks. -
I have noticed problems with BroadcastReceiver on my application shut down see:
[https://forum.qt.io/topic/162708/running-a-profiled-app-on-x86_64-android-emulator-qt6.9.1/15]
Have I set up the "registerReceiver" correctly ?
Having read some Qt documents I reduced th C++ code to:
#ifdef ANDROID jdouble batteryStatus() { jdouble batteryLevel=-1.0; batteryLevel = QJniObject::callStaticMethod<jdouble>("com/home/QtTest4/QtBattery","getBatteryPercentage"); return batteryLevel; } #endif
This did not help the return value is still 0.0.
-
Having read some Qt documents I reduced th C++ code to:
#ifdef ANDROID jdouble batteryStatus() { jdouble batteryLevel=-1.0; batteryLevel = QJniObject::callStaticMethod<jdouble>("com/home/QtTest4/QtBattery","getBatteryPercentage"); return batteryLevel; } #endif
This did not help the return value is still 0.0.
Discovered a possible problem:
When I run the app I get this logged in the QtCreator debugger:
app=com.home.QtTest4
W/default : java.lang.ClassNotFoundException: Didn't find class "com.home.QtTest4.QtBattery" on path: DexPathList[[zip file "/data/app/~~cyksSJNg5CvNsUTpniskXw==/com.home.QtTest4-DhBmss0nc93tjcVIRFtFdQ==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]My AndroidManifest.xml file starts with:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.home.QtTest4" android:installLocation="auto" android:versionCode="115" android:versionName="QtTest4D">
AndroidManifest.xml and QtBattery.java locations:
/work/Qt-Android/QtTest4/android/AndroidManifest.xml
/work/Qt-Android/QtTest4/android/src/QtBattery.javaInvocation of Java method in my C++ file:
batteryLevel = QJniObject::callStaticMethod<jdouble>("com/home/QtTest4/QtBattery","getBatteryPercentage");
I have no clue has to what is wrong, any suggestions please.
-
Discovered a possible problem:
When I run the app I get this logged in the QtCreator debugger:
app=com.home.QtTest4
W/default : java.lang.ClassNotFoundException: Didn't find class "com.home.QtTest4.QtBattery" on path: DexPathList[[zip file "/data/app/~~cyksSJNg5CvNsUTpniskXw==/com.home.QtTest4-DhBmss0nc93tjcVIRFtFdQ==/base.apk"],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64]]My AndroidManifest.xml file starts with:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.home.QtTest4" android:installLocation="auto" android:versionCode="115" android:versionName="QtTest4D">
AndroidManifest.xml and QtBattery.java locations:
/work/Qt-Android/QtTest4/android/AndroidManifest.xml
/work/Qt-Android/QtTest4/android/src/QtBattery.javaInvocation of Java method in my C++ file:
batteryLevel = QJniObject::callStaticMethod<jdouble>("com/home/QtTest4/QtBattery","getBatteryPercentage");
I have no clue has to what is wrong, any suggestions please.
-
S SMF-Qt has marked this topic as solved