Android Service using QAndroidService

  • Hello,

    I am trying to create an Android Service in QT that will give other Android apps the chance to communicate with it. Given that we have the class in QT I thought that it is enough to extend it, un-comment the manifest service part and run the app. Otherwise I am not sure why do we have that class or how to use it.
    If I try to connect to the custom service from another app, I can at least start it using the bindService method which returns true(so I guess it works) but the onBind method from QT of my service never gets called.

    I used some of the code written here:

    My main file looks like this:

    class MyBinder : public QAndroidBinder
        bool onTransact(int code, const QAndroidParcel &data, const QAndroidParcel &reply, QAndroidBinder::CallType flags) override
           // never gets called but it is no surprise for me since the onBind method is not called either
            qDebug() << "~~~  onTransact" << code << int(flags);
            switch (code) {
            case 1: {
                QAndroidBinder binder = data.readBinder();
                qDebug() << "~~~  onTransact client sent its binder !";
                qDebug() << "~~~  onTransact client sent unknow data" << data.readData();
            return true;
    // my service, I would like to do stuff in this service on the QT side if possible
    class MyService : public QAndroidService
        MyService(int &argc, char **argv) : QAndroidService(argc, argv) {}
        QAndroidBinder *onBind(const QAndroidIntent &/*intent*/) override
            // this method is not called ever
            qInfo() << "ServiceXXXL -> ~~~ create onBind !!!";
            return new MyBinder();
    int main(int argc, char *argv[])
          // this method gets called whenever I call bindService() from another android app
         MyService app(argc, argv); // I am pretty sure this is not ok
        return app.exec();

    Is there a working sample of how one can create a Service in QT and actually do stuff in it while allowing other apps or Java native code to bind to it?

    Thank you very much for any help,

    PS: I am new to QT, usually I write Java/Kotlin Android and Swift iOS apps so I am sorry if the question is too "light" :)

  • Moderators

    QAndroidService class is just a substitution for a QCoreApplication, but doesn't create a android service.

    see this:

  • Hello @raven-worx and thanks for your answer. Ok I got it wrong then, I thought I could use the onBind method somehow and link that MyService QT class to the service or to the service.

    Since it wraps most of the Service methods, including onBind, how can I make the onBind method to fire?

    I already checked that blog post but it does not cover what I need, I am already able to start the QtService from another android app but I can't figure out how to also bind to it in QT.

    Any thoughts are much appreciated! :)

  • Hello, there's no other ideas on this? :)

  • Moderators

    What exactly are you trying to achieve?
    Does this help?

  • Hello @raven-worx and thanks once again for your time.

    I am trying to create an Android Service using QT creator that will be able to communicate through bluetooth to some devices and that will enable other Android applications to use it to get certain data from it.

    Like I said above, I managed to enable a Service from QT Creator and also start it from another Android application but unfortunately it is the java QService that is being generated by QT creator at build time and I can't actually handle my bluetooth/logic stuff in it because I don't have access to it from QT (or I don't know how to implement it properly).
    I hoped that by extending the QAndroidService I will be able to override the onBind and do the business logic there but it seems that it doesn't work as I thought and I can't see a good article(that works) on how to communicate to a Service in QT.

    I hope I explained clearly what I would like to achieve, if not please let me know :).

  • Moderators

    so it seems you want a bound service right?
    If so it should be enough to sublcass QAndroidService and in it's onBind() method return a custom QAndroidBinder instance which does the RPC call results.

    I've never done that yet though.

  • @raven-worx yep, that's what I wanted and I already tried that but the onBind method doesn't get called for some reasons... I pasted what I tried above in the post and also the github repo that I copied.

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.