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. QTimer::singleShot() not fire in Android background
Forum Updated to NodeBB v4.3 + New Features

QTimer::singleShot() not fire in Android background

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 4 Posters 1.2k 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.
  • G Offline
    G Offline
    Gourmet
    wrote on last edited by
    #1

    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?

    KroMignonK 1 Reply Last reply
    0
    • G Gourmet

      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?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @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.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      G 1 Reply Last reply
      1
      • KroMignonK KroMignon

        @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.

        G Offline
        G Offline
        Gourmet
        wrote on last edited by Gourmet
        #3

        @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?

        KroMignonK 1 Reply Last reply
        0
        • G Gourmet

          @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?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #4

          @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!

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          G 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @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!

            G Offline
            G Offline
            Gourmet
            wrote on last edited by Gourmet
            #5

            @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.

            KroMignonK jsulmJ J.HilkJ 3 Replies Last reply
            0
            • G Gourmet

              @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.

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @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.

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              0
              • G Gourmet

                @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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • G Gourmet

                  @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.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #8

                  @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


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  G 1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @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

                    G Offline
                    G Offline
                    Gourmet
                    wrote on last edited by Gourmet
                    #9

                    @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.

                    KroMignonK 1 Reply Last reply
                    0
                    • G Gourmet

                      @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.

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #10

                      @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.

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      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