Uga Buga Uga Buga - 1 year ago 346
C++ Question

How to keep the screen on in Qt for android?

I found a couple of solutions how to do that in Java, but did not find how can I do it in QML or Qt. I know that first I should set the

permission in
. What should I do to make it possible to turn on and off the screen locking from Qt in runtime?

Answer Source

You can use the Qt Android Extras module and use JNI to call the relevant Java function from C++. Something like :

void keepScreenOn() 
    QAndroidJniObject activity = QtAndroid::androidActivity();
    if (activity.isValid()) {
        QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");

        if (window.isValid()) {
            const int FLAG_KEEP_SCREEN_ON = 128;
            window.callObjectMethod("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download