How to start a foreground service in Qt6 ?
Unsolved
Mobile and Embedded
-
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 ?
-
@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.