Is it possible to make a Qt Android app with a UI into a foreground service?
-
What I want do do is to have an app that can keep a network connection open also when it's not in the foreground, and have internal timers running so it can play notification sounds at certain times. When incoming messages come over the network, it updates a local sqlite database and may re-arrange when sounds are to be played.
What's happening now is that as soon as the app is not in the foreground, Android puts it to sleep.
According to ChatGpt Android foreground services are not supported in Qt 6.8.3 or newer.
-
What I want do do is to have an app that can keep a network connection open also when it's not in the foreground, and have internal timers running so it can play notification sounds at certain times. When incoming messages come over the network, it updates a local sqlite database and may re-arrange when sounds are to be played.
What's happening now is that as soon as the app is not in the foreground, Android puts it to sleep.
According to ChatGpt Android foreground services are not supported in Qt 6.8.3 or newer.
@jarle On Android you need a background service to do this. See https://doc.qt.io/qt-6/android-services.html
-
@jarle On Android you need a background service to do this. See https://doc.qt.io/qt-6/android-services.html
-
@jsulm I know I can make a background service. But as far as I know, that runs in a separate process. I am looking for the foreground service functionality, where one process can handle both the UI and the service requirements.
@jarle Take a look at foreground services: https://developer.android.com/develop/background-work/services?hl=en
-
@jarle Take a look at foreground services: https://developer.android.com/develop/background-work/services?hl=en
@jsulm Yes, that is what I want. My question was if it is possible to make a QT app run as a foreground service. I spent half a day experimenting with this, and when I was making sense of an error I got from
startForegroundService()
in Java, ChatGPT told me that a QT 6.8 app can only utilize a background service. Foreground services are not possible. -
@jsulm Yes, that is what I want. My question was if it is possible to make a QT app run as a foreground service. I spent half a day experimenting with this, and when I was making sense of an error I got from
startForegroundService()
in Java, ChatGPT told me that a QT 6.8 app can only utilize a background service. Foreground services are not possible.@jarle If I search in Google for "qt android foreground services" I get the bellow KI result. You can't do it directly with Qt, but you can with some Java code.
"
To use foreground services in a Qt Android application, you need to create a Java service class that extends Service and implements the foreground service logic, including displaying a notification. Qt's Android extras module provides tools for interacting with Android services from your Qt code. Foreground services require a notification to inform the user of their activity, and they are crucial for tasks that need to run even when the app is in the background.
Here's a breakdown of how to implement a Qt Android foreground service:-
Create a Java Service Class:
Extend Service: Create a new Java class that inherits from android.app.Service.
Override onCreate(), onStartCommand(), and onDestroy(): These methods handle service lifecycle events.
Implement startForeground(): In onStartCommand(), you'll call startForeground() to make the service a foreground service and display the notification.
Implement notification logic: Create a notification using android.app.Notification.Builder and display it using startForeground(). The notification should clearly indicate that the service is running and what it's doing. -
Qt Integration:
QAndroid ജavaClass: Use QAndroid ജavaClass to interact with your Java service class from your Qt code.
QAndroid ജavaObject: Create an instance of your Java service class using QAndroid ജavaObject.
Call startForegroundService(): From your Qt code, you can call the Java method that starts the service, which should include calling startForegroundService() with a notification. -
Permissions and Manifest:
FOREGROUND_SERVICE permission:
Add the FOREGROUND_SERVICE permission to your AndroidManifest.xml file. This is required for Android 9 (API level 28) and later.
Foreground service type:
For Android 14 and later, you'll need to declare the specific foreground service type in your manifest."
-
-
@jarle If I search in Google for "qt android foreground services" I get the bellow KI result. You can't do it directly with Qt, but you can with some Java code.
"
To use foreground services in a Qt Android application, you need to create a Java service class that extends Service and implements the foreground service logic, including displaying a notification. Qt's Android extras module provides tools for interacting with Android services from your Qt code. Foreground services require a notification to inform the user of their activity, and they are crucial for tasks that need to run even when the app is in the background.
Here's a breakdown of how to implement a Qt Android foreground service:-
Create a Java Service Class:
Extend Service: Create a new Java class that inherits from android.app.Service.
Override onCreate(), onStartCommand(), and onDestroy(): These methods handle service lifecycle events.
Implement startForeground(): In onStartCommand(), you'll call startForeground() to make the service a foreground service and display the notification.
Implement notification logic: Create a notification using android.app.Notification.Builder and display it using startForeground(). The notification should clearly indicate that the service is running and what it's doing. -
Qt Integration:
QAndroid ജavaClass: Use QAndroid ജavaClass to interact with your Java service class from your Qt code.
QAndroid ജavaObject: Create an instance of your Java service class using QAndroid ജavaObject.
Call startForegroundService(): From your Qt code, you can call the Java method that starts the service, which should include calling startForegroundService() with a notification. -
Permissions and Manifest:
FOREGROUND_SERVICE permission:
Add the FOREGROUND_SERVICE permission to your AndroidManifest.xml file. This is required for Android 9 (API level 28) and later.
Foreground service type:
For Android 14 and later, you'll need to declare the specific foreground service type in your manifest."
@jsulm QT Android extras module was removed in QT 6.
I have tried to make this work by adding a Java class for the service part. When I called
startForegroundService()
, I got this error from AndroidW/Qt JAVA : A QtService tried to start in the same process as an initiated QtActivity. That is not supported. This results in the service functioning as an Android Service detached from Qt.
The manifest was set up to handle a foreground service, and all the permissions the app needed running as a forground service.
When I researched this error, I found nothing useful from Google, but ChatGpt gave this explanation:
"you’ve declared your Qt-based service to run in the same Linux process as your QtActivity, so Qt will refuse to spin up its own event loop inside the service. Instead your stub will simply behave like a plain Java Service and won’t hook into the Qt C++ world—so all of your C++ “keep-alive” logic never runs, and Android kills the process once the UI goes away.... Every QtService must live in its own process. "
That was why I posted here, to ask if it is possible to make a QT 6.8.3 app run as a foreground service under Android. If nobody has done it, or know how to successfully do it, then I just have to move on to plan B.
-