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. How to start a foreground service in Qt6 ?
Forum Updated to NodeBB v4.3 + New Features

How to start a foreground service in Qt6 ?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 3 Posters 473 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.
  • I Offline
    I Offline
    Ivelin
    wrote on last edited by
    #1

    Hello,

    I am well aware how to start a background service in Qt6 since it is fine written in the official documentation, but how do you do that with a foreground service? I have found no information about that in Qt6 anywhere!

    Here's a simple example for implementing a background service:

    BootService.java

    package tech.secureme.services;
    
    import android.content.Context;
    import android.content.Intent;
    import org.qtproject.qt.android.bindings.QtService;
    import android.content.Intent;
    
    public class BootService extends QtService {
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int ret = super.onStartCommand(intent, flags, startId);
    
            return START_STICKY;
        }
    
        public static void serviceStart(android.content.Context context) {
            android.content.Intent pQtAndroidService = new android.content.Intent(context, BootService.class);
            pQtAndroidService.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(pQtAndroidService);
        }
    
        public static void serviceStop(android.content.Context context) {
            android.content.Intent pQtAndroidService = new android.content.Intent(context, BootService.class);
            context.stopService(pQtAndroidService);
        }
    z
    }
    

    BootService.h

    #ifndef BOOT_SERVICE_H
    #define BOOT_SERVICE_H
    
    #include <QtCore/QObject>
    class BootService : public QObject
    {
    private:
        BootService();
        ~BootService();
        static BootService *instance;
    
    public:
        void start_service();
    
        static BootService *getInstance();
    };
    
    #endif // BOOT_SERVICE_H
    

    BootService.cpp

    
    
    #include "BootService.h"
    #include <QtCore/private/qandroidextras_p.h>
    
    BootService *BootService::instance = nullptr;
    
    BootService::BootService() {}
    
    BootService::~BootService() {}
    
    BootService *BootService::getInstance() {
        if (instance == nullptr)
            instance = new BootService();
        return instance;
    }
    
    void BootService::start_service() {
        QJniObject::callStaticMethod<void>("tech.secureme.services.BootService",
                                           "serviceStart",
                                           "(Landroid/content/Context;)V",
                                           QNativeInterface::QAndroidApplication::context());
    }
    

    main.cpp

    #include <QGuiApplication>
    #include "services/BootService.h"
    #include <QtCore/private/qandroidextras_p.h>
    int main(int argc, char *argv[])
    {
    
    #if defined(Q_OS_ANDROID)
        for (int i = 1; i < argc; i++)
        {
            if (argv[i])
            {
                if (QString::fromLocal8Bit(argv[i]) == "--service")
                {
                    QAndroidService app(argc, argv);
                    // do something
                    return app.exec();
                }
            }
        }
    #endif
        QGuiApplication app(argc, argv, true);
    
    #if defined(Q_OS_ANDROID)
    
        BootService::getInstance()->start_service();
    #endif
    
        return app.exec();
    }
    

    AndroidManifest.xml

    ... unnecessary code to show
            <service android:process=":qt_service" android:name=".services.BootService">
                <meta-data android:name="android.app.lib_name"
                    android:value="-- %%INSERT_APP_LIB_NAME%% --" />
                <meta-data android:name="android.app.arguments" android:value="--service" />
                <meta-data android:name="android.app.background_running" android:value="true" />
            </service>
    

    But how do I turn this into a foreground service ? Could someone please explain me ?

    L 1 Reply Last reply
    1
    • I Ivelin

      Hello,

      I am well aware how to start a background service in Qt6 since it is fine written in the official documentation, but how do you do that with a foreground service? I have found no information about that in Qt6 anywhere!

      Here's a simple example for implementing a background service:

      BootService.java

      package tech.secureme.services;
      
      import android.content.Context;
      import android.content.Intent;
      import org.qtproject.qt.android.bindings.QtService;
      import android.content.Intent;
      
      public class BootService extends QtService {
      
          @Override
          public void onCreate() {
              super.onCreate();
          }
      
          @Override
          public void onDestroy() {
              super.onDestroy();
          }
      
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
              int ret = super.onStartCommand(intent, flags, startId);
      
              return START_STICKY;
          }
      
          public static void serviceStart(android.content.Context context) {
              android.content.Intent pQtAndroidService = new android.content.Intent(context, BootService.class);
              pQtAndroidService.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startService(pQtAndroidService);
          }
      
          public static void serviceStop(android.content.Context context) {
              android.content.Intent pQtAndroidService = new android.content.Intent(context, BootService.class);
              context.stopService(pQtAndroidService);
          }
      z
      }
      

      BootService.h

      #ifndef BOOT_SERVICE_H
      #define BOOT_SERVICE_H
      
      #include <QtCore/QObject>
      class BootService : public QObject
      {
      private:
          BootService();
          ~BootService();
          static BootService *instance;
      
      public:
          void start_service();
      
          static BootService *getInstance();
      };
      
      #endif // BOOT_SERVICE_H
      

      BootService.cpp

      
      
      #include "BootService.h"
      #include <QtCore/private/qandroidextras_p.h>
      
      BootService *BootService::instance = nullptr;
      
      BootService::BootService() {}
      
      BootService::~BootService() {}
      
      BootService *BootService::getInstance() {
          if (instance == nullptr)
              instance = new BootService();
          return instance;
      }
      
      void BootService::start_service() {
          QJniObject::callStaticMethod<void>("tech.secureme.services.BootService",
                                             "serviceStart",
                                             "(Landroid/content/Context;)V",
                                             QNativeInterface::QAndroidApplication::context());
      }
      

      main.cpp

      #include <QGuiApplication>
      #include "services/BootService.h"
      #include <QtCore/private/qandroidextras_p.h>
      int main(int argc, char *argv[])
      {
      
      #if defined(Q_OS_ANDROID)
          for (int i = 1; i < argc; i++)
          {
              if (argv[i])
              {
                  if (QString::fromLocal8Bit(argv[i]) == "--service")
                  {
                      QAndroidService app(argc, argv);
                      // do something
                      return app.exec();
                  }
              }
          }
      #endif
          QGuiApplication app(argc, argv, true);
      
      #if defined(Q_OS_ANDROID)
      
          BootService::getInstance()->start_service();
      #endif
      
          return app.exec();
      }
      

      AndroidManifest.xml

      ... unnecessary code to show
              <service android:process=":qt_service" android:name=".services.BootService">
                  <meta-data android:name="android.app.lib_name"
                      android:value="-- %%INSERT_APP_LIB_NAME%% --" />
                  <meta-data android:name="android.app.arguments" android:value="--service" />
                  <meta-data android:name="android.app.background_running" android:value="true" />
              </service>
      

      But how do I turn this into a foreground service ? Could someone please explain me ?

      L Offline
      L Offline
      leekh9047
      wrote on last edited by
      #2

      @Ivelin Hey any luck with foreground service? I
      am trying to do this too but no doc/reference

      1 Reply Last reply
      0
      • E Offline
        E Offline
        EyeOhTee
        wrote on last edited by
        #3

        @Ivelin @leekh9047 Any luck with this?
        I'm struggling with getting the background service to run in a separate process. When I run in the same process, it crashes. Ultimately I'd like to make the service a foreground service that owns a reference to my backend.

        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