2

I have Qt application and need to keep screen from dimming while application is running. I placed following code in the beginning of main() in main.cpp:

#ifdef ANDROID
    // disable screen lock on android devices
    QAndroidJniObject activity = QtAndroid::androidActivity();
    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);
        }
    }
#endif

Which pretty much came from: How to keep the screen on in Qt for android?, except that I use callMethod<void> instead of callObjectMethod.

Problem is, when application is started, it crashes with huge Java log, I suspect this is relevant part of it:

F/art     (26455): art/runtime/runtime.cc:289] Pending exception android.view.ViewRootImpl$CalledFromWrongThreadException thrown by 'unknown throw location'
F/art     (26455): art/runtime/runtime.cc:289] android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.ViewRootImpl.checkThread() (ViewRootImpl.java:6357)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.ViewRootImpl.requestLayout() (ViewRootImpl.java:874)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.View.requestLayout() (View.java:17476)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams) (View.java:11477)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.WindowManagerGlobal.updateViewLayout(android.view.View, android.view.ViewGroup$LayoutParams) (WindowManagerGlobal.java:305)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.WindowManagerImpl.updateViewLayout(android.view.View, android.view.ViewGroup$LayoutParams) (WindowManagerImpl.java:91)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.app.Activity.onWindowAttributesChanged(android.view.WindowManager$LayoutParams) (Activity.java:2596)
F/art     (26455): art/runtime/runtime.cc:289]   at void org.qtproject.qt5.android.bindings.QtActivity.onWindowAttributesChanged(android.view.WindowManager$LayoutParams) (QtActivity.java:1385)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.Window.dispatchWindowAttributesChanged(android.view.WindowManager$LayoutParams) (Window.java:836)
F/art     (26455): art/runtime/runtime.cc:289]   at void com.android.internal.policy.impl.PhoneWindow.dispatchWindowAttributesChanged(android.view.WindowManager$LayoutParams) (PhoneWindow.java:3993)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.Window.setFlags(int, int) (Window.java:813)
F/art     (26455): art/runtime/runtime.cc:289]   at void android.view.Window.addFlags(int) (Window.java:771)

I added proper permission (android.permission.WAKE_LOCK) to AndroidManifest.xml. Problem seems to be caused by addFlags() being run outside its main view?I googled quite a bit and couldn't find anyone else having the same issue.

Thanks, Ivan

Community
  • 1
  • 1
Ivan
  • 81
  • 4

3 Answers3

2
#if defined(Q_OS_ANDROID)
QAndroidJniObject activity = QtAndroid::androidActivity();
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);
    }
    QAndroidJniEnvironment env; if (env->ExceptionCheck()) { env->ExceptionClear(); } //Clear any possible pending exceptions.
}
#endif

Got it from here, works for me on 5.1 android well

aneox
  • 21
  • 4
1

I ended up doing this in Java instead.

Here is java code:

package org.qtproject.visualization;

import org.qtproject.qt5.android.bindings.*;
import android.os.Bundle;
import android.view.WindowManager;

public class ScreenOnActivity extends QtActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
        super.onCreate(savedInstanceState);
    }
}

This is then integrated into the rest of application.

Ivan
  • 81
  • 4
0

When deployed on Android, Qt does not actually run in the main application (Java) thread. As android.view.Window.addFlags must be called from Java main thread, it will be hard for you to go back to this thread to have this code work.

The best is to use a WakeLock to prevent the system from going to sleep mode. here is a post explaining how to do it.

This will prevent the screen from dimming while application is running.

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151