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