Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Qt6 Gettting Android Battery Data.
Forum Updated to NodeBB v4.3 + New Features

Qt6 Gettting Android Battery Data.

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 1 Posters 108 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SMF-QtS Offline
    SMF-QtS Offline
    SMF-Qt
    wrote last edited by SMF-Qt
    #1

    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.

    SMF-QtS 1 Reply Last reply
    0
    • SMF-QtS SMF-Qt

      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.

      SMF-QtS Offline
      SMF-QtS Offline
      SMF-Qt
      wrote last edited by
      #2

      @SMF-Qt

      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 ?

      SMF-QtS 1 Reply Last reply
      0
      • SMF-QtS SMF-Qt

        @SMF-Qt

        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 ?

        SMF-QtS Offline
        SMF-QtS Offline
        SMF-Qt
        wrote last edited by
        #3

        @SMF-Qt

        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.

        SMF-QtS 1 Reply Last reply
        0
        • SMF-QtS SMF-Qt

          @SMF-Qt

          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.

          SMF-QtS Offline
          SMF-QtS Offline
          SMF-Qt
          wrote last edited by
          #4

          @SMF-Qt

          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.java

          Invocation 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.

          SMF-QtS 1 Reply Last reply
          0
          • SMF-QtS SMF-Qt

            @SMF-Qt

            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.java

            Invocation 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.

            SMF-QtS Offline
            SMF-QtS Offline
            SMF-Qt
            wrote last edited by
            #5

            @SMF-Qt

            Found the solution:

            changed the static method call in my C++ file to:

            batteryLevel = QJniObject::callStaticMethod<jdouble>("QtBattery","getBatteryPercentage",QNativeInterface::QAndroidApplication::context());
            

            And it now works.

            1 Reply Last reply
            2
            • SMF-QtS SMF-Qt has marked this topic as solved

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved