Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to disable screensaver on Qt6.9.1 Android App
Forum Updated to NodeBB v4.3 + New Features

How to disable screensaver on Qt6.9.1 Android App

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 2 Posters 53 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SMF-QtS Online
    SMF-QtS Online
    SMF-Qt
    wrote last edited by
    #1

    I am trying to stop the screen going off while my qt6 app is running, I found this code for Qt5 but the QtAndroidExtras is no longer in Qt6.
    Is it possible to make this code work with Qt 6.9.1 ?

    #include <QtAndroidExtras/QAndroidJniEnvironment>
    #include <QtAndroidExtras/QtAndroidExtras>

    QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
    if (activity.isValid()) {
    QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
    if (window.isValid()) {
    const int FLAG_KEEP_SCREEN_ON = 128;
    window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
    }
    }

    //Clear any possible pending exceptions.
    QAndroidJniEnvironment env;
    if (env->ExceptionCheck()) {
    env->ExceptionClear();
    }

    jsulmJ 1 Reply Last reply
    0
    • SMF-QtS SMF-Qt

      I am trying to stop the screen going off while my qt6 app is running, I found this code for Qt5 but the QtAndroidExtras is no longer in Qt6.
      Is it possible to make this code work with Qt 6.9.1 ?

      #include <QtAndroidExtras/QAndroidJniEnvironment>
      #include <QtAndroidExtras/QtAndroidExtras>

      QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
      if (activity.isValid()) {
      QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
      if (window.isValid()) {
      const int FLAG_KEEP_SCREEN_ON = 128;
      window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
      }
      }

      //Clear any possible pending exceptions.
      QAndroidJniEnvironment env;
      if (env->ExceptionCheck()) {
      env->ExceptionClear();
      }

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @SMF-Qt See:

      • https://www.qt.io/blog/platform-apis-in-qt-6
      • https://doc-snapshots.qt.io/qt6-dev/native-interfaces.html
      • https://doc-snapshots.qt.io/qt6-dev/qnativeinterface.html

      Last one has some classes for Android.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      SMF-QtS 1 Reply Last reply
      0
      • jsulmJ jsulm

        @SMF-Qt See:

        • https://www.qt.io/blog/platform-apis-in-qt-6
        • https://doc-snapshots.qt.io/qt6-dev/native-interfaces.html
        • https://doc-snapshots.qt.io/qt6-dev/qnativeinterface.html

        Last one has some classes for Android.

        SMF-QtS Online
        SMF-QtS Online
        SMF-Qt
        wrote last edited by SMF-Qt
        #3

        @jsulm
        This compiles and runs:

        int main(int argc, char *argv[])
        {
        #ifdef ANDROID
        QJniObject activity = QNativeInterface::QAndroidApplication::context();
        if(activity.isValid())
        {
        activity.callMethod<void>("setRequestedOrientation", "(I)V", 0);
        QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
        if (window.isValid())
        {
        const int FLAG_KEEP_SCREEN_ON = 128;
        window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
        }
        QJniEnvironment env;
        if (env->ExceptionCheck())
        {
        env->ExceptionClear();
        }
        }
        ...

        The call to fix the orientation into landscape works (solution from a previous topic).
        The call to keep the screen on does not work.
        The exception check returns false (if that is relevant ???).

        Am I setting the correct flags ?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved