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. QT 5.4 How do I know the Battery Level on Android?
Forum Updated to NodeBB v4.3 + New Features

QT 5.4 How do I know the Battery Level on Android?

Scheduled Pinned Locked Moved Mobile and Embedded
3 Posts 2 Posters 2.0k Views 1 Watching
  • 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.
  • H Offline
    H Offline
    helpforprogrammer
    wrote on last edited by
    #1

    QT 5.4 How do I know the Battery Level on Android?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sanchir Kartiev
      wrote on last edited by
      #2

      I think so you can resolve this problem using JNI.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        helpforprogrammer
        wrote on last edited by
        #3

        I have an error java.lang.RuntimeException: Can not create handler inside thread that has not called Looper.prepare ()

        @import android.content.Intent;
        import android.content.IntentFilter;
        import android.os.BatteryManager;
        import android.app.Activity;
        import android.os.Bundle;

        public class NotificationClient extends org.qtproject.qt5.android.bindings.QtActivity
        {

        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;
        }
        
        public static float notify2()
        {
            try{
            NotificationClient obj1 = new NotificationClient();
            return obj1.getBatteryLevel();
            }catch(Exception e){
            System.out.println( e.toString() );
            }
        }
        

        }@

        1 Reply Last reply
        0

        • Login

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