Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Is it possible to make a Qt Android app with a UI into a foreground service?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to make a Qt Android app with a UI into a foreground service?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 2 Posters 154 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jarle
    wrote last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • J jarle

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @jarle On Android you need a background service to do this. See https://doc.qt.io/qt-6/android-services.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • jsulmJ jsulm

        @jarle On Android you need a background service to do this. See https://doc.qt.io/qt-6/android-services.html

        J Offline
        J Offline
        jarle
        wrote last edited by
        #3

        @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.

        jsulmJ 1 Reply Last reply
        0
        • J jarle

          @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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote last edited by
          #4

          @jarle Take a look at foreground services: https://developer.android.com/develop/background-work/services?hl=en

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @jarle Take a look at foreground services: https://developer.android.com/develop/background-work/services?hl=en

            J Offline
            J Offline
            jarle
            wrote last edited by
            #5

            @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.

            jsulmJ 1 Reply Last reply
            0
            • J jarle

              @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.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote last edited by
              #6

              @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:

              1. 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.

              2. 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.

              3. 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.

              "

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              0
              • jsulmJ jsulm

                @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:

                1. 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.

                2. 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.

                3. 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.

                "

                J Offline
                J Offline
                jarle
                wrote last edited by
                #7

                @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 Android

                W/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.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved