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
16 Posts 3 Posters 356 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 Offline
    SMF-QtS Offline
    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 Offline
        SMF-QtS Offline
        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 ?

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

          @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 ?

          SMF-QtS Offline
          SMF-QtS Offline
          SMF-Qt
          wrote last edited by
          #4

          @SMF-Qt

          I found this bit of java which I think does what I want:

          PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
          PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
          wl.acquire();
          ...
          // screen and CPU will stay awake during this section
          ...
          wl.release();

          Can this be used with QJniObject class if so how do I do it ?

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

            @SMF-Qt

            I found this bit of java which I think does what I want:

            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
            wl.acquire();
            ...
            // screen and CPU will stay awake during this section
            ...
            wl.release();

            Can this be used with QJniObject class if so how do I do it ?

            SMF-QtS Offline
            SMF-QtS Offline
            SMF-Qt
            wrote last edited by
            #5

            @SMF-Qt
            Having read some Qt5 solutions for this problem, and the associated Android devlopment and Qt6 Jni information I have come up with this solution for setting the screen in Landscape mode and stopping the screen timeout.

            The landscape bit works but the screen lock only seems to work on the simulator.
            The code is running through the appropriate path in all cases as indicated by the flags a,b,c,d which I display on the screen of my app (all 1's) as a debug aid:

            Code1.png

            Sorry for using a png image but the stupid spam filter would not let me post the actual code!!!!

            Can anyone see any mistakes in what I have written that stop it working on real hardware (could it be a permissions issue)?

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

              @SMF-Qt
              Having read some Qt5 solutions for this problem, and the associated Android devlopment and Qt6 Jni information I have come up with this solution for setting the screen in Landscape mode and stopping the screen timeout.

              The landscape bit works but the screen lock only seems to work on the simulator.
              The code is running through the appropriate path in all cases as indicated by the flags a,b,c,d which I display on the screen of my app (all 1's) as a debug aid:

              Code1.png

              Sorry for using a png image but the stupid spam filter would not let me post the actual code!!!!

              Can anyone see any mistakes in what I have written that stop it working on real hardware (could it be a permissions issue)?

              SMF-QtS Offline
              SMF-QtS Offline
              SMF-Qt
              wrote last edited by SMF-Qt
              #6

              @SMF-Qt

              Added a line of code to try and address any permission issues, but this was ineffectual:

                  int a=0,b=0,c=0,d=0;
                  QFuture permission_request = QtAndroidPrivate::requestPermission("android.permission.SCREEN_DIM_WAKE_LOCK"); // new code
                  QJniObject wakeLock;
              
              
              SMF-QtS 1 Reply Last reply
              0
              • SMF-QtS SMF-Qt

                @SMF-Qt

                Added a line of code to try and address any permission issues, but this was ineffectual:

                    int a=0,b=0,c=0,d=0;
                    QFuture permission_request = QtAndroidPrivate::requestPermission("android.permission.SCREEN_DIM_WAKE_LOCK"); // new code
                    QJniObject wakeLock;
                
                
                SMF-QtS Offline
                SMF-QtS Offline
                SMF-Qt
                wrote last edited by
                #7

                @SMF-Qt

                Someone suggested this solution but again the isValid() tests pass but the result fails.

                    #ifdef ANDROID
                    int a=0,b=0,c=0,d=0;
                    QJniObject window;
                    QJniObject activity = QNativeInterface::QAndroidApplication::context();
                    if(activity.isValid())
                    {
                        a=1;
                        activity.callMethod<void>("setRequestedOrientation", "(I)V", 0);
                        window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
                        if (window.isValid())
                        {
                            b=1;
                            window.callMethod<void>("addFlags", "(I)V","WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)");
                        }
                    }
                    #endif
                
                
                SMF-QtS 1 Reply Last reply
                0
                • Miszkurka2000M Offline
                  Miszkurka2000M Offline
                  Miszkurka2000
                  wrote last edited by
                  #8

                  Hi everyone. I'm new here out of curiosity.
                  I don't know if I can be of any help, but try asking Grok.

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

                    @SMF-Qt

                    Someone suggested this solution but again the isValid() tests pass but the result fails.

                        #ifdef ANDROID
                        int a=0,b=0,c=0,d=0;
                        QJniObject window;
                        QJniObject activity = QNativeInterface::QAndroidApplication::context();
                        if(activity.isValid())
                        {
                            a=1;
                            activity.callMethod<void>("setRequestedOrientation", "(I)V", 0);
                            window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
                            if (window.isValid())
                            {
                                b=1;
                                window.callMethod<void>("addFlags", "(I)V","WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)");
                            }
                        }
                        #endif
                    
                    
                    SMF-QtS Offline
                    SMF-QtS Offline
                    SMF-Qt
                    wrote last edited by SMF-Qt
                    #9

                    @SMF-Qt

                    Noticed this output from QtCreator while debugging my test App, could it be relevant to my problem ?

                    /EGL_emulation: app_time_stats: avg=500.33ms min=498.20ms max=501.49ms count=3
                    W/qtMainLoopThrea: type=1400 audit(0.0:9194): avc: denied { read } for name="usb" dev="tmpfs" ino=713 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:usb_device:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
                    W/qtMainLoopThrea: type=1400 audit(0.0:9195): avc: denied { read } for name="/" dev="tmpfs" ino=1 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:device:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
                    W/qtMainLoopThrea: type=1400 audit(0.0:9196): avc: denied { read } for name="devices" dev="sysfs" ino=4521 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
                    D/EGL_emulation: app_time_stats: avg=500.41ms min=499.71ms max=501.11ms count=2

                    I expect the usb open I have in my code to fail in an Android context it is looking for a particular usb dongle in a Linux context. But this again seems to be a permissons issue at the lowest level???

                    1 Reply Last reply
                    0
                    • Miszkurka2000M Miszkurka2000

                      Hi everyone. I'm new here out of curiosity.
                      I don't know if I can be of any help, but try asking Grok.

                      SMF-QtS Offline
                      SMF-QtS Offline
                      SMF-Qt
                      wrote last edited by
                      #10

                      @Miszkurka2000

                      Thanks, but Grok was unable to help.

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

                        @Miszkurka2000

                        Thanks, but Grok was unable to help.

                        SMF-QtS Offline
                        SMF-QtS Offline
                        SMF-Qt
                        wrote last edited by
                        #11

                        @SMF-Qt

                        Ok giving up on a programatic C++ solution and going down the AndroidManifest.xml route.

                        The setting of the screen in Landscape works <added: android:screenOrientation="landscape"> to the AndroidManifest.xml file.

                        From the Android devlopers site [https://developer.android.com/develop/background-work/background-tasks/awake/screen-on] they recommend adding:

                        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:keepScreenOn="true">
                        </RelativeLayout>
                        

                        I have tried this code in the AndroidManifest.xml file. However this results in a build error:

                        AAPT: error: unexpected element <RelativeLayout> found in <manifest>

                        Can anyone suggest the correct syntax and/or the correct file to include it in?

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

                          @SMF-Qt

                          Ok giving up on a programatic C++ solution and going down the AndroidManifest.xml route.

                          The setting of the screen in Landscape works <added: android:screenOrientation="landscape"> to the AndroidManifest.xml file.

                          From the Android devlopers site [https://developer.android.com/develop/background-work/background-tasks/awake/screen-on] they recommend adding:

                          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                              android:layout_width="match_parent"
                              android:layout_height="match_parent"
                              android:keepScreenOn="true">
                          </RelativeLayout>
                          

                          I have tried this code in the AndroidManifest.xml file. However this results in a build error:

                          AAPT: error: unexpected element <RelativeLayout> found in <manifest>

                          Can anyone suggest the correct syntax and/or the correct file to include it in?

                          SMF-QtS Offline
                          SMF-QtS Offline
                          SMF-Qt
                          wrote last edited by SMF-Qt
                          #12

                          @SMF-Qt

                          Found this piece of java code which I have built in my project. How do I get it executed as part of an activity in my AndroidManifest.xml file.

                          import android.app.Activity;
                          import android.os.Bundle;
                          import android.view.WindowManager;
                          
                              public class KeepScreenOn extends Activity {
                                  @Override
                                  protected void onCreate(Bundle savedInstanceState) {
                                      super.onCreate(savedInstanceState);
                              
                                      // Keep the screen on
                                      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                                  }
                              }
                          
                          SMF-QtS 1 Reply Last reply
                          0
                          • SMF-QtS SMF-Qt

                            @SMF-Qt

                            Found this piece of java code which I have built in my project. How do I get it executed as part of an activity in my AndroidManifest.xml file.

                            import android.app.Activity;
                            import android.os.Bundle;
                            import android.view.WindowManager;
                            
                                public class KeepScreenOn extends Activity {
                                    @Override
                                    protected void onCreate(Bundle savedInstanceState) {
                                        super.onCreate(savedInstanceState);
                                
                                        // Keep the screen on
                                        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                                    }
                                }
                            
                            SMF-QtS Offline
                            SMF-QtS Offline
                            SMF-Qt
                            wrote last edited by
                            #13

                            @SMF-Qt

                            Add :

                            <activity android:name="KeepScreenOn">
                            </activity>
                            

                            To AndroidManifest.xml compiles but does not work!!?

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

                              @SMF-Qt

                              Add :

                              <activity android:name="KeepScreenOn">
                              </activity>
                              

                              To AndroidManifest.xml compiles but does not work!!?

                              SMF-QtS Offline
                              SMF-QtS Offline
                              SMF-Qt
                              wrote last edited by
                              #14

                              @SMF-Qt

                              Someone suggested adding

                              setContentView(R.layout.activity_main);
                              
                              

                              Above the getwindow() call in the java file.

                              but this errors on building with:

                              /work/Qt-Android/QtTest4/build/Android_Qt_6_9_1_qt_androidX86_Clang_x86_64-Debug/android-build/src/KeepScreenOn.java:9: error: package R does not exist
                              setContentView(R.layout.activity_main);

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

                                @SMF-Qt

                                Someone suggested adding

                                setContentView(R.layout.activity_main);
                                
                                

                                Above the getwindow() call in the java file.

                                but this errors on building with:

                                /work/Qt-Android/QtTest4/build/Android_Qt_6_9_1_qt_androidX86_Clang_x86_64-Debug/android-build/src/KeepScreenOn.java:9: error: package R does not exist
                                setContentView(R.layout.activity_main);

                                SMF-QtS Offline
                                SMF-QtS Offline
                                SMF-Qt
                                wrote last edited by
                                #15

                                @SMF-Qt

                                Could not get anything to work apart from adding:

                                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
                                

                                To the QtActivity.java file onCreate method. Which does work. Not an elegant solution but it will do until I can find a better way.

                                1 Reply Last reply
                                0
                                • SMF-QtS Offline
                                  SMF-QtS Offline
                                  SMF-Qt
                                  wrote last edited by
                                  #16

                                  I have tried various ways to get "getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)" running however the only solution that works for me is to put it in the "onCreate" method in the QtActivity java class (Qt6.9.1) . It really needs to be conditional but I don't know enough about java to do that. Any suggestions welcome.

                                  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