Adding an Android Service: when using "Service in Separate Process", main() is only called once
-
I wanted to add a dummy Service to my Hello World QML application. So I
- triggered "Create templates"
- added a service in the AndroidManifest.xml
<service android:name=".QtAndroidService" android:process=":qtService"> <meta-data android:name="android.app.background_running" android:value="true"/> <meta-data android:name="android.app.arguments" android:value="-service"/> <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/> </service>
- created a (dummy) QtAndroidService.java (from here) in android/src/org/qtproject/example (I checked that it gets compiled)
- I added a
qInfo() << "main argc=" << argc;
as first line to main()
I was expecting that main() would be called twice now
- once with argc=1 to start the application
- once with argc=2 with argument "-service" which I can use to start the service
But main() is only called once apparently?
Am I missing something or is this a bug?I'm testing on an emulator and verified the logs both in QtCreator and in adb logcat.
As far as I know, main() was called twice in Qt5.12 in this case? -
@Marc_Van_Daele I know it's been a long time, but I am working on a similar issue.
I think what you're looking for is a manual invocation of the Java Service object start like they do here
i.e. change the following code to match your custom service:
auto activity = QJniObject(QNativeInterface::QAndroidApplication:context()); QAndroidIntent serviceIntent(activity.object(), "org/qtproject/example/qtandroidservice/QtAndroidService"); QJniObject result = activity.callObjectMethod( "startService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", serviceIntent.handle().object());
The documentations is not so friendly, I spent a lot of time thinking the service would auto start by virtue of the manifest file but that has not been the case for me.
Also, the Qt Creator app output doesn't "link in" to the 2nd process started for the service. I had to fire up adb shell logcat - once I did, I saw the service process outputs