Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

JNI function not working in qml



  • androidBatteryStateChanged(level, onCharge)

    not working qml file please assist me

    project file :

    QT += qml quick androidextras

    SOURCES += main.cpp
    androidbatterystate.cpp

    RESOURCES += qml.qrc

    OTHER_FILES +=
    android/AndroidManifest.xml
    android/src/com/falsinsoft/example/batterylevel/AppActivity.java
    android/src/com/falsinsoft/example/batterylevel/BatteryLevelListener.java

    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android

    DISTFILES +=

    HEADERS +=
    androidbatterystate.h

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    header file:

    #ifndef ANDROIDBATTERYSTATE_H
    #define ANDROIDBATTERYSTATE_H
    #include <QtAndroidExtras>
    #include <QObject>
    #include <jni.h>
    class AndroidBatteryState : public QObject
    {
    Q_OBJECT

    public:
    explicit AndroidBatteryState(QObject *parent = nullptr);

    signals:
    void BatteryLevel(int level, bool onCharge);

    public slots:
    void InitializeForAndroid();

    private:

    };

    #endif // ANDROIDBATTERYSTATE_H

    //+++++++++++++++++++++++++++++++++++++++++++++++

    source file:

    #include "androidbatterystate.h"

    AndroidBatteryState::AndroidBatteryState(QObject *parent) : QObject(parent)
    {

    }

    void AndroidBatteryStateChanged(JNIEnv *env, jobject thiz, jint level, jboolean onCharge)
    {
    Q_UNUSED(env)
    Q_UNUSED(thiz)
    qDebug()<< "Battery Charge: "<< level << "%";

    }

    void AndroidBatteryState::InitializeForAndroid()
    {
    JNINativeMethod methods[] = {
    {
    "BatteryStateChanged",
    "(IZ)V",
    reinterpret_cast<void*>(AndroidBatteryStateChanged)
    }
    };

    QAndroidJniObject javaClass("com/falsinsoft/example/batterylevel/BatteryLevelListener");
    QAndroidJniEnvironment env;
    
    jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
    
    env->RegisterNatives(objectClass,
        methods,
        sizeof(methods) / sizeof(methods[0]));
    env->DeleteLocalRef(objectClass);
    
    QAndroidJniObject::callStaticMethod<void>("com/falsinsoft/example/batterylevel/BatteryLevelListener", "InstallBatteryListener");
    

    qDebug()<< "Initialized";

    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++

    main ccp file:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QtAndroidExtras>
    #include "androidbatterystate.h"
    #include <QQmlContext>

    int main(int argc, char *argv[])
    {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    AndroidBatteryState androidbatterystate;
    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("BatteryState",&androidbatterystate);

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
       if (engine.rootObjects().isEmpty())
           return -1;
    

    androidbatterystate.InitializeForAndroid();

    return app.exec();
    

    }

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++

    main qml file:

    import QtQuick 2.7
    import QtQuick.Controls 2.1
    import QtQuick.Controls.Material 2.1
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.3

    Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Android Battery Level")
    color: "blue"
    id: root
    function androidBatteryStateChanged(level, onCharge)
    {

        batteryLevel.text = "Battery level: " + level + "%";
        batteryStatus.text = "On charging: " + (onCharge ? "Yes" : "No");
    }
    
    
    
    Column {
        anchors.centerIn: parent
        spacing: 20
        Label {
    
            font.pixelSize: 20
            color: "white"
            text: "Battery Level"
        }
        Label {
            id: batteryLevel
            font.pixelSize: 20
            color: "white"
    
        }
        Label {
            id: batteryStatus
            font.pixelSize: 20
            color: "white"
    
        }
    }
    

    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    AppActivity class

    package com.falsinsoft.example.batterylevel;

    import android.app.Activity;

    public class AppActivity extends org.qtproject.qt5.android.bindings.QtActivity
    {
    public AppActivity()
    {
    BatteryLevelListener.Init(this);
    }
    }
    //+++++++++++++++++++++++++++++++++

    BatteryLevelListener class

    package com.falsinsoft.example.batterylevel;

    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.IntentFilter;
    import android.content.Intent;
    import android.os.BatteryManager;
    import android.content.Context;

    public class BatteryLevelListener
    {
    private static Activity m_ActivityInstance;

    public static void Init(Activity ActivityInstance)
    {
        m_ActivityInstance = ActivityInstance;
    }
    
    public static native void BatteryStateChanged(int Level, boolean OnCharge);
    
    public static void InstallBatteryListener()
    {
        IntentFilter BatteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    
        BroadcastReceiver BatteryLevelReceiver = new BroadcastReceiver()
        {
            public void onReceive(Context context, Intent intent)
            {
                boolean OnCharge = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0) ? false : true;
                int Level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int Scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    
                if(Level >= 0 && Scale > 0)
                {
                    BatteryStateChanged((Level * 100) / Scale, OnCharge);
                }
            }
        };
    
        m_ActivityInstance.registerReceiver(BatteryLevelReceiver, BatteryLevelFilter);
    }
    

    }


Log in to reply