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

QTimer::singleShot() not fire in Android background



  • Qt 5.12, Android 7.1.
    In AndroidManifest.XML I wrote this:

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

    The main code has follwing:

        QTimer::singleShot(100, this, SLOT(goingWork()));
    

    But before this line application runs another one app which appears active. After this single shot won't fire until I switch to main app manually. Right after I switch manually - the timer fires. This is very surprising and looks like a bug. I think if application is allowed to work in background - timer must work too.

    Is anybody knowing something more about this?



  • @Gourmet AFAIK, an Activity should never be allowed to run in background. This is not recommander with Qt and may crashes the App.
    If you need background work, it is much better to use a Service.



  • @KroMignon said in QTimer::singleShot() not fire in Android background:

    @Gourmet AFAIK, an Activity should never be allowed to run in background. This is not recommander with Qt and may crashes the App.
    If you need background work, it is much better to use a Service.

    AFAIK Qt's android.app.background_running=true forces application to work as Service indeed. At least this worked fine in my previous apps. But the question is rather different - why timer does not shoot in this mode?



  • @Gourmet said in QTimer::singleShot() not fire in Android background:

    But the question is rather different - why timer does not shoot in this mode?

    For my understanding of Android workflow, an Activity cannot work in background ==> https://developer.android.com/guide/components/activities/activity-lifecycle

    So when going in background main thread is paused and event queue cannot work.
    And according to Qt remarks in AndroidManifest.xml:

    Warning: changing this value to true may cause unexpected crashes if the application still try to draw after "applicationStateChanged(Qt::ApplicationSuspended)" signal is sent!



  • @KroMignon 21 my other apps work in background. They play music as well cause they all are music players. I cannot tell yet if they continue draw cause I did not see them covered by other apps. But they do not stop draw - they show colorful music visualizations actively and they have moving-rotating items on QGraphicsScene. Music is played by Android internal music engine started from Qt. Therefore it's interesting idea create some semitransparent app and run it over my music apps - to see if they still can draw. All of them have android.app.background_running = true. If they continue draw - then event queue and main Qt cycle work in background.



  • @Gourmet it seems that you have more experience than me. I don't believe this is the right way to use Activities and that you should split your App in an Activity and at least one background Service, but that is just my opinion, based on my understanding of Android documentation: https://developer.android.com/guide/components/activities/process-lifecycle

    But perhaps I am wrong.


  • Lifetime Qt Champion

    @Gourmet Shouldn't you use https://developer.android.com/guide/components/services on Android for background tasks? There is no guarantee as far as I know that an application will run if it is in background.


  • Moderators

    @Gourmet said in QTimer::singleShot() not fire in Android background:

    All of them have android.app.background_running = true. If they continue draw - then event queue and main Qt cycle work in background.

    thats a miss conception, setting android.app.background_running to true extends the time before android shuts your "background running app" off it's by no means a full blown service that is running at all times!

    If you go to the title screen of andoird your app will keep on doing functions for a while (it's based of a specific amount of time, not sure what that is exactly) but as soon as you launch an other app all bets are of and Android os my kill your app at any moment



  • @J-Hilk said in QTimer::singleShot() not fire in Android background:

    If you go to the title screen of andoird your app will keep on doing functions for a while (it's based of a specific amount of time, not sure what that is exactly) but as soon as you launch an other app all bets are of and Android os my kill your app at any moment

    This is true even for Services which are not active and are not visible. That means - Service which does not have at least one running thread and does not have taskbar icon CAN be killed.

    But this all does NOT prevent QTimer fire singleShot and execute connected method. It MUST work in background. My application WERE NOT killed - just QTimer paused suddenly.

    I am not sure if QTimer::singleShot() will work even in Service. Surely not.



  • @Gourmet said in QTimer::singleShot() not fire in Android background:

    I am not sure if QTimer::singleShot() will work even in Service. Surely not.

    It works, I am using it often. A Android background Service is like a linux daemon, it runs all the time.
    You just have to care about not using same process for Activity and Service.


Log in to reply