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
17 Posts 4 Posters 2.4k 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 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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
                      • X Offline
                        X Offline
                        X_m7
                        wrote last edited by
                        #17

                        I think the missing link might be that the addFlags call wasn't happening in the Android main thread, in my case I have a QML application so I created a C++ QObject called ScreenOnKeeper with a single boolean property called enabled, so the QML side creates an instance of ScreenOnKeeper and sets its enabled property to true to keep the screen on (or false to allow the screen to turn off again), and below is what I have in the write method for that property:

                        void ScreenOnKeeper::setEnabled(const bool yes) {
                        #ifdef ANDROID
                            QNativeInterface::QAndroidApplication::runOnAndroidMainThread([yes]() {
                                QJniObject activity = QNativeInterface::QAndroidApplication::context();
                                if(activity.isValid())
                                {
                                    QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
                                    if (window.isValid())
                                    {
                                        // if getting the flag dynamically fails use the constant 128
                                        // from https://developer.android.com/reference/android/view/WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON
                                        int keep_screen_on_flag = QJniObject::getStaticField<int>("android/view/WindowManager$LayoutParams", "FLAG_KEEP_SCREEN_ON");
                        
                                        window.callMethod<void>(yes ? "addFlags" : "clearFlags", keep_screen_on_flag);
                                    }
                                }
                            }).waitForFinished();
                        #endif
                            m_enabled = yes;
                            emit enabledChanged();
                        }
                        

                        The main difference between this and the code snippets you've posted is that the QJniObject stuff is all contained in a function given to QNativeInterface::QAndroidApplication::runOnAndroidMainThread(), for me this works on both the emulator with Android 16 as well as a Samsung Galaxy A26 with One UI 8.0 (based on Android 16 as well).

                        I am using Qt 6.10.1 though, not sure if any of the relevant syntax has changed between that and 6.9.x.

                        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