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. keep screen ON on Qt for Android
Qt 6.11 is out! See what's new in the release blog

keep screen ON on Qt for Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
androidqt 5.6javaqml
5 Posts 3 Posters 5.0k Views 2 Watching
  • 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.
  • Z Offline
    Z Offline
    Zakaria
    wrote on last edited by
    #1

    hi,
    I like to know where is the main Activity java file so i can add this line

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

    from this link http://developer.android.com/training/scheduling/wakelock.html

    to keep the screen on when the app is running without an extra permission

    thank you :)

    1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Do you mean QtActivity.java ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Z 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        Do you mean QtActivity.java ?

        Z Offline
        Z Offline
        Zakaria
        wrote on last edited by
        #3

        @SGaist
        yes i found the QtActivity.java
        but when i add the line as showen on the link : http://developer.android.com/training/scheduling/wakelock.html

        i'm not able to run the APK

            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
        	/////// i added the line here 
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        	//////
                try {
                    m_activityInfo = getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
                    for (Field f : Class.forName("android.R$style").getDeclaredFields()) {
                        if (f.getInt(null) == m_activityInfo.getThemeResource()) {
                            QT_ANDROID_THEMES = new String[] {f.getName()};
                            QT_ANDROID_DEFAULT_THEME = f.getName();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    finish();
                    return;
                }
        
                try {
                    setTheme(Class.forName("android.R$style").getDeclaredField(QT_ANDROID_DEFAULT_THEME).getInt(null));
                } catch (Exception e) {
                    e.printStackTrace();
                }
        
                if (Build.VERSION.SDK_INT > 10) {
                    try {
                        requestWindowFeature(Window.class.getField("FEATURE_ACTION_BAR").getInt(null));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    requestWindowFeature(Window.FEATURE_NO_TITLE);
                }
        
                if (QtApplication.m_delegateObject != null && QtApplication.onCreate != null) {
                    QtApplication.invokeDelegateMethod(QtApplication.onCreate, savedInstanceState);
                    return;
                }
        
                m_displayDensity = getResources().getDisplayMetrics().densityDpi;
        
                ENVIRONMENT_VARIABLES += "\tQT_ANDROID_THEME=" + QT_ANDROID_DEFAULT_THEME
                                      + "/\tQT_ANDROID_THEME_DISPLAY_DPI=" + m_displayDensity + "\t";
        
                if (null == getLastNonConfigurationInstance()) {
                    // if splash screen is defined, then show it
                    if (m_activityInfo.metaData.containsKey("android.app.splash_screen_drawable"))
                        getWindow().setBackgroundDrawableResource(m_activityInfo.metaData.getInt("android.app.splash_screen_drawable"));
                    else
                        getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000));
        
                    if (m_activityInfo.metaData.containsKey("android.app.background_running")
                        && m_activityInfo.metaData.getBoolean("android.app.background_running")) {
                        ENVIRONMENT_VARIABLES += "QT_BLOCK_EVENT_LOOPS_WHEN_SUSPENDED=0\t";
                    } else {
                        ENVIRONMENT_VARIABLES += "QT_BLOCK_EVENT_LOOPS_WHEN_SUSPENDED=1\t";
                    }
        
                    if (m_activityInfo.metaData.containsKey("android.app.auto_screen_scale_factor")
                        && m_activityInfo.metaData.getBoolean("android.app.auto_screen_scale_factor")) {
                        ENVIRONMENT_VARIABLES += "QT_AUTO_SCREEN_SCALE_FACTOR=1\t";
                    }
        			
                    startApp(true);
                }
        		
            }
        

        thank you

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Do you mean QtActivity.java ?

          Z Offline
          Z Offline
          Zakaria
          wrote on last edited by
          #4

          @SGaist
          thank you i solved it by adding 'this.' to the line of code in the QtActivity.java

          @Override
              public void onCreate(Bundle savedInstanceState)
              {
                  super.onCreate(savedInstanceState);
          		this.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
          ....
          }
          

          alternative here :
          https://forum.qt.io/topic/57625/solved-keep-android-5-screen-on

          R 1 Reply Last reply
          1
          • Z Zakaria

            @SGaist
            thank you i solved it by adding 'this.' to the line of code in the QtActivity.java

            @Override
                public void onCreate(Bundle savedInstanceState)
                {
                    super.onCreate(savedInstanceState);
            		this.getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            ....
            }
            

            alternative here :
            https://forum.qt.io/topic/57625/solved-keep-android-5-screen-on

            R Offline
            R Offline
            Ramon Faria
            wrote on last edited by
            #5

            @Zakaria Thanks! Still working in 5.9.2.

            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