How to disable screensaver on Qt6.9.1 Android App
-
Added a line of code to try and address any permission issues, but this was ineffectual:
int a=0,b=0,c=0,d=0; QFuture permission_request = QtAndroidPrivate::requestPermission("android.permission.SCREEN_DIM_WAKE_LOCK"); // new code QJniObject wakeLock;Someone suggested this solution but again the isValid() tests pass but the result fails.
#ifdef ANDROID int a=0,b=0,c=0,d=0; QJniObject window; QJniObject activity = QNativeInterface::QAndroidApplication::context(); if(activity.isValid()) { a=1; activity.callMethod<void>("setRequestedOrientation", "(I)V", 0); window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); if (window.isValid()) { b=1; window.callMethod<void>("addFlags", "(I)V","WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)"); } } #endif -
Hi everyone. I'm new here out of curiosity.
I don't know if I can be of any help, but try asking Grok. -
Someone suggested this solution but again the isValid() tests pass but the result fails.
#ifdef ANDROID int a=0,b=0,c=0,d=0; QJniObject window; QJniObject activity = QNativeInterface::QAndroidApplication::context(); if(activity.isValid()) { a=1; activity.callMethod<void>("setRequestedOrientation", "(I)V", 0); window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); if (window.isValid()) { b=1; window.callMethod<void>("addFlags", "(I)V","WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)"); } } #endifNoticed this output from QtCreator while debugging my test App, could it be relevant to my problem ?
/EGL_emulation: app_time_stats: avg=500.33ms min=498.20ms max=501.49ms count=3
W/qtMainLoopThrea: type=1400 audit(0.0:9194): avc: denied { read } for name="usb" dev="tmpfs" ino=713 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:usb_device:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
W/qtMainLoopThrea: type=1400 audit(0.0:9195): avc: denied { read } for name="/" dev="tmpfs" ino=1 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:device:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
W/qtMainLoopThrea: type=1400 audit(0.0:9196): avc: denied { read } for name="devices" dev="sysfs" ino=4521 scontext=u:r:untrusted_app:s0:c213,c256,c512,c768 tcontext=u:object_r:sysfs:s0 tclass=dir permissive=0 app=org.qtproject.example.QtTest4D
D/EGL_emulation: app_time_stats: avg=500.41ms min=499.71ms max=501.11ms count=2I expect the usb open I have in my code to fail in an Android context it is looking for a particular usb dongle in a Linux context. But this again seems to be a permissons issue at the lowest level???
-
Hi everyone. I'm new here out of curiosity.
I don't know if I can be of any help, but try asking Grok.Thanks, but Grok was unable to help.
-
Thanks, but Grok was unable to help.
Ok giving up on a programatic C++ solution and going down the AndroidManifest.xml route.
The setting of the screen in Landscape works <added: android:screenOrientation="landscape"> to the AndroidManifest.xml file.
From the Android devlopers site [https://developer.android.com/develop/background-work/background-tasks/awake/screen-on] they recommend adding:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"> </RelativeLayout>I have tried this code in the AndroidManifest.xml file. However this results in a build error:
AAPT: error: unexpected element <RelativeLayout> found in <manifest>
Can anyone suggest the correct syntax and/or the correct file to include it in?
-
Ok giving up on a programatic C++ solution and going down the AndroidManifest.xml route.
The setting of the screen in Landscape works <added: android:screenOrientation="landscape"> to the AndroidManifest.xml file.
From the Android devlopers site [https://developer.android.com/develop/background-work/background-tasks/awake/screen-on] they recommend adding:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"> </RelativeLayout>I have tried this code in the AndroidManifest.xml file. However this results in a build error:
AAPT: error: unexpected element <RelativeLayout> found in <manifest>
Can anyone suggest the correct syntax and/or the correct file to include it in?
Found this piece of java code which I have built in my project. How do I get it executed as part of an activity in my AndroidManifest.xml file.
import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; public class KeepScreenOn extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Keep the screen on getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } -
Found this piece of java code which I have built in my project. How do I get it executed as part of an activity in my AndroidManifest.xml file.
import android.app.Activity; import android.os.Bundle; import android.view.WindowManager; public class KeepScreenOn extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Keep the screen on getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } } -
Add :
<activity android:name="KeepScreenOn"> </activity>To AndroidManifest.xml compiles but does not work!!?
Someone suggested adding
setContentView(R.layout.activity_main);Above the getwindow() call in the java file.
but this errors on building with:
/work/Qt-Android/QtTest4/build/Android_Qt_6_9_1_qt_androidX86_Clang_x86_64-Debug/android-build/src/KeepScreenOn.java:9: error: package R does not exist
setContentView(R.layout.activity_main); -
Someone suggested adding
setContentView(R.layout.activity_main);Above the getwindow() call in the java file.
but this errors on building with:
/work/Qt-Android/QtTest4/build/Android_Qt_6_9_1_qt_androidX86_Clang_x86_64-Debug/android-build/src/KeepScreenOn.java:9: error: package R does not exist
setContentView(R.layout.activity_main);Could not get anything to work apart from adding:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)To the QtActivity.java file onCreate method. Which does work. Not an elegant solution but it will do until I can find a better way.
-
I have tried various ways to get "getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)" running however the only solution that works for me is to put it in the "onCreate" method in the QtActivity java class (Qt6.9.1) . It really needs to be conditional but I don't know enough about java to do that. Any suggestions welcome.
-
I think the missing link might be that the addFlags call wasn't happening in the Android main thread, in my case I have a QML application so I created a C++ QObject called
ScreenOnKeeperwith a single boolean property calledenabled, so the QML side creates an instance ofScreenOnKeeperand sets itsenabledproperty totrueto keep the screen on (orfalseto allow the screen to turn off again), and below is what I have in the write method for that property:void ScreenOnKeeper::setEnabled(const bool yes) { #ifdef ANDROID QNativeInterface::QAndroidApplication::runOnAndroidMainThread([yes]() { QJniObject activity = QNativeInterface::QAndroidApplication::context(); if(activity.isValid()) { QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); if (window.isValid()) { // if getting the flag dynamically fails use the constant 128 // from https://developer.android.com/reference/android/view/WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON int keep_screen_on_flag = QJniObject::getStaticField<int>("android/view/WindowManager$LayoutParams", "FLAG_KEEP_SCREEN_ON"); window.callMethod<void>(yes ? "addFlags" : "clearFlags", keep_screen_on_flag); } } }).waitForFinished(); #endif m_enabled = yes; emit enabledChanged(); }The main difference between this and the code snippets you've posted is that the
QJniObjectstuff is all contained in a function given toQNativeInterface::QAndroidApplication::runOnAndroidMainThread(), for me this works on both the emulator with Android 16 as well as a Samsung Galaxy A26 with One UI 8.0 (based on Android 16 as well).I am using Qt 6.10.1 though, not sure if any of the relevant syntax has changed between that and 6.9.x.