Search code examples
androidqtserviceandroid-service

Android QtService doesn't call override methods like onCreate on a separated process


I'm working with this:

QMake version 3.1
Using Qt version 5.12.8 in /usr/lib/x86_64-linux-gnu
minSdkVersion 21
targetSdkVersion 29
Tested on two Android 10

When I run a android QtService the override methods like onCreate are never executed if the service run in a separated process with the option android:process=":rs on the XML definition. The Qt library is executed correctly, because I can see debug info on logcat but if I add any code on the onCreate or onStartCommand it never executes.

My service code:

package net.altermundi.elrepoio

import android.app.ActivityManager
import android.content.Context
import android.content.Intent

import org.qtproject.qt5.android.bindings.QtService


class RetroShareServiceAndroid : QtService() {


    override fun onCreate() {
        System.out.println("salkmdsa onCreate")

        super.onCreate()
    }

    override fun onDestroy() {
        System.out.println("salkmdsa onDestroy")

        super.onDestroy()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        System.out.println("salkmdsa onStartCommand")
        // Do some work
        return super.onStartCommand(intent, flags, startId)
    }

    companion object {
        fun start(ctx: Context) {
            ctx.startService(Intent(ctx, RetroShareServiceAndroid::class.java))
        }

        fun stop(ctx: Context) {
            ctx.stopService(Intent(ctx, RetroShareServiceAndroid::class.java))
        }

        fun isRunning(ctx: Context): Boolean {
            val manager = ctx.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            for (service in manager.getRunningServices(Integer.MAX_VALUE))
                if (RetroShareServiceAndroid::class.java!!.getName().equals(service.service.getClassName()))
                    return true
            return false
        }
    }
}

As we see above, I added some println (print line) execution. This code, or any other, never is executed if run in a separated process.

My service definition on the android manifest:

<service android:name=".RetroShareServiceAndroid" android:label="RetroShare Service" android:process=":rs> 

            <!-- Qt Application to launch -->
            <meta-data android:name="android.app.lib_name" android:value="retroshare-service"/>

            <!-- Ministro -->
            <meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
            <meta-data android:name="android.app.repository" android:value="default"/>
            <meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
            <meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
            <!-- Deploy Qt libs as part of package -->
            <meta-data android:name="android.app.bundle_local_qt_libs" android:value="1"/>
            <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
            <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
            <!-- Run with local libs -->
            <meta-data android:name="android.app.use_local_qt_libs" android:value="1"/>
            <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
            <meta-data android:name="android.app.load_local_libs" android:value="plugins/platforms/android/libqtforandroid.so:plugins/bearer/libqandroidbearer.so"/>
            <meta-data android:name="android.app.load_local_jars" android:value="jar/QtAndroid.jar:jar/QtAndroidExtras.jar:jar/QtAndroidBearer.jar"/>
            <meta-data android:name="android.app.static_init_classes" android:value=""/>
            <!--  Messages maps -->
            <meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
            <meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
            <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
            <!-- Messages maps -->

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

I don't paste the Main activity here because I guess that the code is not relevant, anyway can be found here.


Solution

  • Can I say to myself stupid?

    I can't see the println strings on the IDE console because, the process is separated.

    If I look using logcat I saw them.

    Case closed