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 do I prompt the launch of an Android Service in the background (Qt5.7)?
QtWS25 Last Chance

How do I prompt the launch of an Android Service in the background (Qt5.7)?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5.7androidservice
12 Posts 5 Posters 10.1k 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.
  • R Offline
    R Offline
    refaQtor
    wrote on 19 May 2016, 22:13 last edited by
    #1

    I'm attempting to launch a long running background service on Android with Qt 5.7. The limited documentation on this new feature (in bold below) seemed obvious enough.

    I am successfully building and launching this application on Android, and getting qDebug output in QtCreator, though I only expect to get the console output for the primary Activity instance of the application.

    But, I seem to be missing some magical incantation that gets Android to also launch the background service at all.

    I've put snips of the AndroidManifest.xml in line with the steps to indicate my interpretation, in hopes someone might spot my mistake or misunderstanding...

    Android Services
    Starting with Qt 5.7 you can use Qt to create Android services. A service is a component that runs in background, so, it has no user interface. It is useful to perform long-time operations (for example log GPS, wait for social media notifications, and so on). A service will continue to run even if the application that started it exits.
    To create a service, you need to do the following steps:

    1. Uncomment the service part of your AndroidManifest.xml
    ---so, I uncommented this...all the actual non-comments xml lines between these tags

    <service .... > "..."    </service>
    

    2. Make sure the service tag contains an android:process=":some_name" attribute. It is needed to force the service to run in a separate process than the activity.
    ---and put ":mynetworkservice" in the android:process attribute

     <service android:process=":mynetworkservice" android:name="org.qtproject.qt5.android.bindings.QtService">
    

    3. If you're using the same application (.so file) for activity and also for service, you need to use android.app.arguments meta-data to pass some arguments to your main function in order to know which one is which.
    ---and left "-service" for the argument. I'm using two instances of the same application, behaving differently based on the switch- see Qt code below

                <!-- Application arguments -->
                <meta-data android:name="android.app.arguments" android:value="-service"/>
                <!-- Application arguments -->
    

    4. Enable background running. Uncomment android.app.background_running meta-data and set it to true (android:value="true" ).
    ---and left the uncommented value as "true"

                <!-- Background running -->
                <meta-data android:name="android.app.background_running" android:value="true"/>
                <!-- Background running -->
    

    Qt will load the .so file defined in android.app.lib_name meta-data, will call the main function with all the arguments set in android.app.arguments meta-data.

    additionally, I added this permission, which is adequate to prove the initial execution does work

    <uses-permission android:name="android.permission.INTERNET"/>
    

    With the following code I get the CLIENT path of this code to launch every time, but Android doesn't seem to attempt to launch it gain as a background service (I think that I set it to run the exact same executable as background service in the AndroidManifest.xml) at all, with or without a switch.

    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QString iam("unset");
    
        if (QCoreApplication::arguments().count() > 1){ //any switch at all
            iam = "SERVER";
                netcat(iam);
        } else {
            iam = "CLIENT";
                netcat(iam);
            QQmlApplicationEngine engine;
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        }
    
        return app.exec();
    }
    
    void netcat(QString iam)
    {
        // sending a simple POST to a netcat instance on my local network
        QNetworkAccessManager *manager = new QNetworkAccessManager(0);
        QNetworkRequest request;
        request.setUrl(QUrl("http://192.168.1.107:3030"));
        request.setRawHeader("User-Agent", iam.toLocal8Bit());
        QNetworkReply *reply = manager->post(request, iam.toLocal8Bit());
    }
    

    I confirmed, also, that there is no background task showing up in Android Settings->Apps->Running.

    Thanks for looking...
    Can anyone point out what I am missing?

    S 1 Reply Last reply 28 Jun 2016, 19:20
    0
    • R refaQtor
      19 May 2016, 22:13

      I'm attempting to launch a long running background service on Android with Qt 5.7. The limited documentation on this new feature (in bold below) seemed obvious enough.

      I am successfully building and launching this application on Android, and getting qDebug output in QtCreator, though I only expect to get the console output for the primary Activity instance of the application.

      But, I seem to be missing some magical incantation that gets Android to also launch the background service at all.

      I've put snips of the AndroidManifest.xml in line with the steps to indicate my interpretation, in hopes someone might spot my mistake or misunderstanding...

      Android Services
      Starting with Qt 5.7 you can use Qt to create Android services. A service is a component that runs in background, so, it has no user interface. It is useful to perform long-time operations (for example log GPS, wait for social media notifications, and so on). A service will continue to run even if the application that started it exits.
      To create a service, you need to do the following steps:

      1. Uncomment the service part of your AndroidManifest.xml
      ---so, I uncommented this...all the actual non-comments xml lines between these tags

      <service .... > "..."    </service>
      

      2. Make sure the service tag contains an android:process=":some_name" attribute. It is needed to force the service to run in a separate process than the activity.
      ---and put ":mynetworkservice" in the android:process attribute

       <service android:process=":mynetworkservice" android:name="org.qtproject.qt5.android.bindings.QtService">
      

      3. If you're using the same application (.so file) for activity and also for service, you need to use android.app.arguments meta-data to pass some arguments to your main function in order to know which one is which.
      ---and left "-service" for the argument. I'm using two instances of the same application, behaving differently based on the switch- see Qt code below

                  <!-- Application arguments -->
                  <meta-data android:name="android.app.arguments" android:value="-service"/>
                  <!-- Application arguments -->
      

      4. Enable background running. Uncomment android.app.background_running meta-data and set it to true (android:value="true" ).
      ---and left the uncommented value as "true"

                  <!-- Background running -->
                  <meta-data android:name="android.app.background_running" android:value="true"/>
                  <!-- Background running -->
      

      Qt will load the .so file defined in android.app.lib_name meta-data, will call the main function with all the arguments set in android.app.arguments meta-data.

      additionally, I added this permission, which is adequate to prove the initial execution does work

      <uses-permission android:name="android.permission.INTERNET"/>
      

      With the following code I get the CLIENT path of this code to launch every time, but Android doesn't seem to attempt to launch it gain as a background service (I think that I set it to run the exact same executable as background service in the AndroidManifest.xml) at all, with or without a switch.

      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          QString iam("unset");
      
          if (QCoreApplication::arguments().count() > 1){ //any switch at all
              iam = "SERVER";
                  netcat(iam);
          } else {
              iam = "CLIENT";
                  netcat(iam);
              QQmlApplicationEngine engine;
              engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          }
      
          return app.exec();
      }
      
      void netcat(QString iam)
      {
          // sending a simple POST to a netcat instance on my local network
          QNetworkAccessManager *manager = new QNetworkAccessManager(0);
          QNetworkRequest request;
          request.setUrl(QUrl("http://192.168.1.107:3030"));
          request.setRawHeader("User-Agent", iam.toLocal8Bit());
          QNetworkReply *reply = manager->post(request, iam.toLocal8Bit());
      }
      

      I confirmed, also, that there is no background task showing up in Android Settings->Apps->Running.

      Thanks for looking...
      Can anyone point out what I am missing?

      S Offline
      S Offline
      Schluchti
      wrote on 28 Jun 2016, 19:20 last edited by
      #2

      @refaQtor Could you make it work? I am currently facing the same problem. I uploaded my progress to github: https://github.com/bbernhard/qtandroidservices_example

      Any help is really appreciated!

      Want to read more about Qt?

      https://gympulsr.com/blog/qt/

      Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

      R 1 Reply Last reply 29 Jun 2016, 19:23
      0
      • S Schluchti
        28 Jun 2016, 19:20

        @refaQtor Could you make it work? I am currently facing the same problem. I uploaded my progress to github: https://github.com/bbernhard/qtandroidservices_example

        Any help is really appreciated!

        R Offline
        R Offline
        refaQtor
        wrote on 29 Jun 2016, 19:23 last edited by
        #3

        @Schluchti I'm just getting back to it now. I've had a few exchanges with BogDan Vatra regarding this (on his KDAB article https://www.kdab.com/qt-android-episode-7/ ) search for "Shannon on June 3, 2016 at 11:17 pm said:" to see the discussion.

        I'll take a look through your repo, and see if it will all become clear. good luck!

        S 1 Reply Last reply 29 Jun 2016, 21:02
        0
        • R refaQtor
          29 Jun 2016, 19:23

          @Schluchti I'm just getting back to it now. I've had a few exchanges with BogDan Vatra regarding this (on his KDAB article https://www.kdab.com/qt-android-episode-7/ ) search for "Shannon on June 3, 2016 at 11:17 pm said:" to see the discussion.

          I'll take a look through your repo, and see if it will all become clear. good luck!

          S Offline
          S Offline
          Schluchti
          wrote on 29 Jun 2016, 21:02 last edited by Schluchti
          #4

          @refaQtor I just commited another change to the github repository. The strange thing is, that I am still not seeing the debug output, but when I go to "settings" on my Android phone to list all running applications I see that "qt_androidservicesexample" (application + service) is running. My assumption is, that the service is up and running but the debug output somehow isn't showing up.
          I am currently trying to extend the example with something similar to the thing you did (the "netcat" thing) to confirm my theory.

          edit: okay, with the netcat trick, I clearly see that the service gets started. This raises the following question: Is this a bug or is it technically not possible to use qDebug() in an android service?

          Want to read more about Qt?

          https://gympulsr.com/blog/qt/

          Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

          R 1 Reply Last reply 30 Jun 2016, 01:44
          0
          • S Schluchti
            29 Jun 2016, 21:02

            @refaQtor I just commited another change to the github repository. The strange thing is, that I am still not seeing the debug output, but when I go to "settings" on my Android phone to list all running applications I see that "qt_androidservicesexample" (application + service) is running. My assumption is, that the service is up and running but the debug output somehow isn't showing up.
            I am currently trying to extend the example with something similar to the thing you did (the "netcat" thing) to confirm my theory.

            edit: okay, with the netcat trick, I clearly see that the service gets started. This raises the following question: Is this a bug or is it technically not possible to use qDebug() in an android service?

            R Offline
            R Offline
            refaQtor
            wrote on 30 Jun 2016, 01:44 last edited by
            #5

            @Schluchti I came up with the netcat thing simply because I didn't figure that the debugger, which launched and connected with the main application would be able to pick up and connect to what, to the qt debugger, appear as android launching another application. I mean, when I used AndroidStudio directly to look at its debugger I see gobs of stuff from other applications. The QtCreator debugger setup, thankfully, seems only to show pertinent messages to the single application under test.
            So, I'm glad my code snips helped you. I'm hopeful that you could see that your service did actually get launched. I'll open my whole sample repo to share, so others don't have this grief.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Schluchti
              wrote on 1 Jul 2016, 15:03 last edited by
              #6

              The netcat thing was really a clever trick, thanks for sharing that ;). I really like the "logging over TCP" approach and I think that this maybe is convenient way to add debugging output from the service part. I think I will spend some time and create a more generalized logging routine based on your idea. :)

              Want to read more about Qt?

              https://gympulsr.com/blog/qt/

              Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

              1 Reply Last reply
              0
              • eirihamE Offline
                eirihamE Offline
                eiriham
                wrote on 27 Jul 2016, 11:13 last edited by
                #7

                This may be a bit off topic, but dont know where else to ask since QtService posts dont really seem to be getting answered.
                Using QtService, will only java code be run in the background or is it possible to run Qt/c++ code as well?

                D 1 Reply Last reply 9 Oct 2019, 18:58
                0
                • M Offline
                  M Offline
                  Mena
                  wrote on 31 May 2017, 14:56 last edited by
                  #8

                  Can you please show us how to make this service run on booting the android phone?

                  S 1 Reply Last reply 31 May 2017, 15:31
                  1
                  • M Mena
                    31 May 2017, 14:56

                    Can you please show us how to make this service run on booting the android phone?

                    S Offline
                    S Offline
                    Schluchti
                    wrote on 31 May 2017, 15:31 last edited by
                    #9

                    @Mena have a look at this article: https://www.kdab.com/qt-android-create-android-service-using-qt/

                    Want to read more about Qt?

                    https://gympulsr.com/blog/qt/

                    Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                    M 1 Reply Last reply 1 Jun 2017, 07:54
                    2
                    • S Schluchti
                      31 May 2017, 15:31

                      @Mena have a look at this article: https://www.kdab.com/qt-android-create-android-service-using-qt/

                      M Offline
                      M Offline
                      Mena
                      wrote on 1 Jun 2017, 07:54 last edited by
                      #10

                      @Schluchti Thank you for your attention.
                      I did check this article and for three days i can't get it to work. I even downloaded the code itself but it is not working too! It doesn't issue any errors at all. However, I cannot see a service running on my android phone.

                      I also downloaded this code:
                      https://github.com/bbernhard/qtandroidservices_example

                      It worked! But, i can not make the service run on boot time.
                      I asked this in the forum here:
                      https://forum.qt.io/topic/79801/can-t-listen-to-android-os-intents-ex-receive_boot_completed

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        Mena
                        wrote on 4 Jun 2017, 11:24 last edited by
                        #11

                        ok, I was able to make the service run at boot time. Here's the solution:
                        https://stackoverflow.com/a/44353506/8086424

                        1 Reply Last reply
                        0
                        • eirihamE eiriham
                          27 Jul 2016, 11:13

                          This may be a bit off topic, but dont know where else to ask since QtService posts dont really seem to be getting answered.
                          Using QtService, will only java code be run in the background or is it possible to run Qt/c++ code as well?

                          D Offline
                          D Offline
                          donp
                          wrote on 9 Oct 2019, 18:58 last edited by
                          #12

                          @eiriham Were you able to get this question answered? My application is using QAndroidService and I would like to run code from Qt/c++? Thanks

                          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