How to keep the screen on in Qt for android?

前端 未结 3 960
孤独总比滥情好
孤独总比滥情好 2021-01-18 02:54

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 WAKE_LOCK permission in

3条回答
  •  情话喂你
    2021-01-18 03:29

    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);
            }
        }
    }
    

提交回复
热议问题