Important: Please read the 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;
               int FLAG_SHOW_WHEN_LOCKED = QAndroidJniObject::getStaticField<jint>(
               int FLAG_DISMISS_KEYGUARD = QAndroidJniObject::getStaticField<jint>(
               int FLAG_KEEP_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
               int FLAG_TURN_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
               int FLAG_ALLOW_LOCK_WHILE_SCREEN_ON = QAndroidJniObject::getStaticField<jint>(
               qDebug() << "MI6 Hello:" << (FLAG_SHOW_WHEN_LOCKED |
                           FLAG_DISMISS_KEYGUARD | FLAG_KEEP_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