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. Adding an Android Service: when using "Service in Separate Process", main() is only called once
QtWS25 Last Chance

Adding an Android Service: when using "Service in Separate Process", main() is only called once

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
qt6.4.1androidemulatorandroid service
3 Posts 2 Posters 660 Views
  • 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.
  • M Offline
    M Offline
    Marc_Van_Daele
    wrote on 18 Dec 2022, 10:58 last edited by Marc_Van_Daele
    #1

    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?

    M 1 Reply Last reply 11 Apr 2023, 07:00
    0
    • M Marc_Van_Daele
      18 Dec 2022, 10:58

      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?

      M Offline
      M Offline
      Malachi
      wrote on 11 Apr 2023, 07:00 last edited by
      #2

      @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

      M 1 Reply Last reply 12 Apr 2023, 16:01
      0
      • M Malachi
        11 Apr 2023, 07:00

        @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

        M Offline
        M Offline
        Marc_Van_Daele
        wrote on 12 Apr 2023, 16:01 last edited by
        #3

        @Malachi I think you are absolutely right!

        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