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. Starting multiple android services
Qt 6.11 is out! See what's new in the release blog

Starting multiple android services

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

    Hello,

    I want to start multiple Android services. In this case, I am trying to start two services. Regardless of the service's type, the first one always works just fine. However, the second one does not start, no matter what.

    Here's what I have:

    main.cpp

        QCoreApplication app(argc, argv, true);
    
        BootService::getInstance()->start_service();
        WebService::getInstance()->start_service();
    
        return app.exec();
    

    BootService.cpp

    ...
    void BootService::start_service()
    {
        QJniObject::callStaticMethod<void>("tech.secureme.services.BootService",
                                           "startService",
                                           "(Landroid/content/Context;)V",
                                           QNativeInterface::QAndroidApplication::context());
    }
    ...
    

    WebService.cpp

    ...
    void WebService::start_service() {
        QJniObject::callStaticMethod<void>("tech.secureme.services.WebService",
                                           "serviceStart",
                                           "(Landroid/content/Context;)V",
                                           QNativeInterface::QAndroidApplication::context());
    }
    ...
    

    BootService.java

    public class BootService extends QtService {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return Service.START_STICKY;
        }
    
        public static void startService(Context ctx) {
            ctx.startService(new Intent(ctx, BootService.class));
        }
    
    
    }
    

    WebService.java

    public class WebService extends QtService {
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            return Service.START_STICKY;
        }
    
        public static void serviceStart(Context ctx) {
            ctx.startService(new Intent(ctx, WebService.class));
        }
    }
    

    AndroidManifest.xml

    ....
            <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>
    
            <service
                    android:process=":qt_service" android:name=".services.WebService">
                <meta-data android:name="android.app.lib_name"
                           android:value="-- %%INSERT_APP_LIB_NAME%% --" />
                <meta-data android:name="android.app.arguments" android:value="--web_service" />
                <meta-data android:name="android.app.background_running" android:value="true" />
            </service>
    ...
    

    Why does the first service always start, but the second one doesn't ? How can I fix that?

    I 1 Reply Last reply
    0
    • I Ivelin has marked this topic as solved on
    • I Ivelin

      Hello,

      I want to start multiple Android services. In this case, I am trying to start two services. Regardless of the service's type, the first one always works just fine. However, the second one does not start, no matter what.

      Here's what I have:

      main.cpp

          QCoreApplication app(argc, argv, true);
      
          BootService::getInstance()->start_service();
          WebService::getInstance()->start_service();
      
          return app.exec();
      

      BootService.cpp

      ...
      void BootService::start_service()
      {
          QJniObject::callStaticMethod<void>("tech.secureme.services.BootService",
                                             "startService",
                                             "(Landroid/content/Context;)V",
                                             QNativeInterface::QAndroidApplication::context());
      }
      ...
      

      WebService.cpp

      ...
      void WebService::start_service() {
          QJniObject::callStaticMethod<void>("tech.secureme.services.WebService",
                                             "serviceStart",
                                             "(Landroid/content/Context;)V",
                                             QNativeInterface::QAndroidApplication::context());
      }
      ...
      

      BootService.java

      public class BootService extends QtService {
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
              return Service.START_STICKY;
          }
      
          public static void startService(Context ctx) {
              ctx.startService(new Intent(ctx, BootService.class));
          }
      
      
      }
      

      WebService.java

      public class WebService extends QtService {
          @Override
          public int onStartCommand(Intent intent, int flags, int startId) {
      
              return Service.START_STICKY;
          }
      
          public static void serviceStart(Context ctx) {
              ctx.startService(new Intent(ctx, WebService.class));
          }
      }
      

      AndroidManifest.xml

      ....
              <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>
      
              <service
                      android:process=":qt_service" android:name=".services.WebService">
                  <meta-data android:name="android.app.lib_name"
                             android:value="-- %%INSERT_APP_LIB_NAME%% --" />
                  <meta-data android:name="android.app.arguments" android:value="--web_service" />
                  <meta-data android:name="android.app.background_running" android:value="true" />
              </service>
      ...
      

      Why does the first service always start, but the second one doesn't ? How can I fix that?

      I Offline
      I Offline
      Ivelin
      wrote on last edited by
      #2

      That was such a silly mistake. I had to rename the android:process attribute in each service to something unique that does not repeat.

      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