How to disable screensaver on Qt6.9.1 Android App
-
I am trying to stop the screen going off while my qt6 app is running, I found this code for Qt5 but the QtAndroidExtras is no longer in Qt6.
Is it possible to make this code work with Qt 6.9.1 ?#include <QtAndroidExtras/QAndroidJniEnvironment>
#include <QtAndroidExtras/QtAndroidExtras>QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
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);
}
}//Clear any possible pending exceptions.
QAndroidJniEnvironment env;
if (env->ExceptionCheck()) {
env->ExceptionClear();
} -
I am trying to stop the screen going off while my qt6 app is running, I found this code for Qt5 but the QtAndroidExtras is no longer in Qt6.
Is it possible to make this code work with Qt 6.9.1 ?#include <QtAndroidExtras/QAndroidJniEnvironment>
#include <QtAndroidExtras/QtAndroidExtras>QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
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);
}
}//Clear any possible pending exceptions.
QAndroidJniEnvironment env;
if (env->ExceptionCheck()) {
env->ExceptionClear();
}@SMF-Qt See:
- https://www.qt.io/blog/platform-apis-in-qt-6
- https://doc-snapshots.qt.io/qt6-dev/native-interfaces.html
- https://doc-snapshots.qt.io/qt6-dev/qnativeinterface.html
Last one has some classes for Android.
-
@SMF-Qt See:
- https://www.qt.io/blog/platform-apis-in-qt-6
- https://doc-snapshots.qt.io/qt6-dev/native-interfaces.html
- https://doc-snapshots.qt.io/qt6-dev/qnativeinterface.html
Last one has some classes for Android.
@jsulm
This compiles and runs:int main(int argc, char *argv[])
{
#ifdef ANDROID
QJniObject activity = QNativeInterface::QAndroidApplication::context();
if(activity.isValid())
{
activity.callMethod<void>("setRequestedOrientation", "(I)V", 0);
QJniObject 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);
}
QJniEnvironment env;
if (env->ExceptionCheck())
{
env->ExceptionClear();
}
}
...The call to fix the orientation into landscape works (solution from a previous topic).
The call to keep the screen on does not work.
The exception check returns false (if that is relevant ???).Am I setting the correct flags ?