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

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

  • I think so you can resolve this problem using JNI.

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


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.