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

Wake up from sleep Android 4.4 Qt



  • I'm designing an app that should turn off screen if a button pressed and wake the device up if a certain package received on TCP/IP. To acheive this the android device can be rooted and have no lock screen.
    My application GUI is written in qml and the TCP server/client and the corresponding handlers written in QT. The screen turn off work out of the box by using Android device admin
    But I had difficult time to figure out a function for waking up part. I used answer from StackExchange - Turn ON Screen programmatically on Android with no lock
    I can receive the packet while the device is in sleep mode and I can set window flag succefully (for exmple FLAG_FULLSCREEN) but the mentioned solution not wake up my device, despite that I am sure the flag has been set.
    Here is my code:

           QAndroidJniObject javaControl;
           QtAndroid::runOnAndroidThread([&javaControl]()
           {
               int FLAG_SHOW_WHEN_LOCKED = QAndroidJniObject::getStaticField<jint>(
                           "android/view/WindowManager$LayoutParams","FLAG_SHOW_WHEN_LOCKED");
               int FLAG_DISMISS_KEYGUARD = QAndroidJniObject::getStaticField<jint>(
                           "android/view/WindowManager$LayoutParams","FLAG_DISMISS_KEYGUARD");
               int FLAG_KEEP_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
                           "android/view/WindowManager$LayoutParams","FLAG_KEEP_SCREEN_ON");
               int FLAG_TURN_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
                           "android/view/WindowManager$LayoutParams","FLAG_TURN_SCREEN_ON");
               int FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
                           "android/view/WindowManager$LayoutParams","FLAG_ALLOW_LOCK_WHILE_SCREEN_ON");
               qDebug() << "MI6 Hello:" << (FLAG_SHOW_WHEN_LOCKED |
                           FLAG_DISMISS_KEYGUARD | FLAG_KEEP_SCREEN_ON |
                           FLAG_TURN_SCREEN_ON | FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
    
               QAndroidJniObject activity = QtAndroid::androidActivity();
               QAndroidJniObject a_window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
               a_window.callMethod<void>("addFlags", "(I)V", FLAG_SHOW_WHEN_LOCKED |
                                       FLAG_DISMISS_KEYGUARD | FLAG_KEEP_SCREEN_ON |
                                       FLAG_TURN_SCREEN_ON | FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
           });
    

    I ran the code in main thread because it is the only way to set a window flag


Log in to reply