Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Keeping a QTcpSocket alive with an Android service



  • How would I keep a QTcpSocket running when the app is suspended?

    I have an Android/iOS app that needs to stay partially running in the background. On Android this means keeping a QTcpSocket running after the app is suspended.

    For Android I have a service that is started with the connection (the connection being a QObject containing QTcpSockets). When the app is about to be suspended, the connection is put into a "background mode" where it only keeps critical sockets open and a pointer is sent to the service for reference:

    #ifdef Q_OS_ANDROID
        connect(appPtr, &QGuiApplication::applicationStateChanged, this, [this, parent](Qt::ApplicationState state)
        {
            if (state == Qt::ApplicationSuspended)
            {
                qDebug() << "Connection pointer: " << (jlong)this;
                m_foregroundParent = parent;
                setParent(nullptr);
                CancelNonEssentials();
                m_inBackground = true;
                QJniObject::callStaticMethod<void>(
                    "BackgroundService",
                    "setConnection",
                    "(J)V",
                    this);
            }
            else if (state == Qt::ApplicationActive)
            {
                QJniObject::callStaticMethod<void>(
                    "BackgroundService",
                    "setConnection",
                    "(J)V",
                    0);
                m_inBackground = false;
                setParent(m_foregroundParent);
            }
        });
        QJniObject::callStaticMethod<void>(
            "BackgroundService",
            "startQtAndroidService",
            "(Landroid/content/Context;)V",
            QNativeInterface::QAndroidApplication::context());
    #endif
    

    Everything works, the service is started, the connection transitions. Only problem is that no data is read until the app re-opens, where it suddenly reads all of the data that was buffered.



  • I follow this title



  • Not sure what that means, but if you know of any solution to accomplish the title, would love to know!



  • Tried using a QtService specifically instead of a regular Android service. And this works, it keeps the socket alive in the background. Only problem is that it never creates the service, causes the screen to go black and outputs this repeatedly:

    E BufferQueueProducer: [SurfaceView - com.agragps.agragps/org.qtproject.qt.android.bindings.QtActivity@ffd32b0@0[20018]#0] query: BufferQueue has been abandoned
    E BufferQueueProducer: [SurfaceView - com.agragps.agragps/org.qtproject.qt.android.bindings.QtActivity@ffd32b0@0[20018]#0] dequeueBuffer: BufferQueue has been abandoned
    I Adreno  : DequeueBuffer: dequeueBuffer failed
    E BufferQueueProducer: [SurfaceView - com.agragps.agragps/org.qtproject.qt.android.bindings.QtActivity@ffd32b0@0[20018]#0] query: BufferQueue has been abandoned
    E BufferQueueProducer: [SurfaceView - com.agragps.agragps/org.qtproject.qt.android.bindings.QtActivity@ffd32b0@0[20018]#0] dequeueBuffer: BufferQueue has been abandoned
    I Adreno  : DequeueBuffer: dequeueBuffer failed
    W libAgraGPS_armeabi-v7a.so: QEGLPlatformContext: eglSwapBuffers failed: 300d
    

    On an older device:

    E BufferQueueProducer: [SurfaceView - com.agragps.agragps/org.qtproject.qt.android.bindings.QtActivity@718e731@0#0] dequeueBuffer: BufferQueue has been abandoned
    W libAgraGPS_armeabi-v7a.so: QEGLPlatformContext: eglSwapBuffers failed: 300d
    

    Made a minimal reproduction and reported it as a bug: https://bugreports.qt.io/browse/QTBUG-99691


Log in to reply