keep screen ON on Qt for Android



  • 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 :)


  • Lifetime Qt Champion

    Hi,

    Do you mean QtActivity.java ?



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.