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. QApplication main loop doesn't work when in background on Android
Forum Updated to NodeBB v4.3 + New Features

QApplication main loop doesn't work when in background on Android

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 3 Posters 3.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Marek
    wrote on last edited by
    #1

    Hi all,

    According to this post and discussion here, main event-loop doesn't work when phone is locked, since Qt 5.3 upwards. And this is feature not bug.

    My problem is that:
    I need to gather GPS position in my app, even when not in active mode, so what I need to do, I have a few questions. I haven't tested this issue on iOS and Windows mobile, but I suppose Qt main event-loop will behave the same. So what options do I have?

    1. I have just now created a new thread and moved class with QTimer into it and it works when app is inactive ;)
    2. There is something also like Android Services mentioned in discussion here but to be honest this looks like far more complicated.

    So in essence, I need also network connectivity, and a few other things, should I move everything not related to GUI to separate thread and it will work ? Network especially. I have event filter installed to tell me when app is in active state and then I can update gui.

    Best Regards
    Marek

    raven-worxR 1 Reply Last reply
    0
    • M Marek

      Hi all,

      According to this post and discussion here, main event-loop doesn't work when phone is locked, since Qt 5.3 upwards. And this is feature not bug.

      My problem is that:
      I need to gather GPS position in my app, even when not in active mode, so what I need to do, I have a few questions. I haven't tested this issue on iOS and Windows mobile, but I suppose Qt main event-loop will behave the same. So what options do I have?

      1. I have just now created a new thread and moved class with QTimer into it and it works when app is inactive ;)
      2. There is something also like Android Services mentioned in discussion here but to be honest this looks like far more complicated.

      So in essence, I need also network connectivity, and a few other things, should I move everything not related to GUI to separate thread and it will work ? Network especially. I have event filter installed to tell me when app is in active state and then I can update gui.

      Best Regards
      Marek

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Marek said in QApplication main loop doesn't work when in background on Android:

      There is something also like Android Services mentioned in discussion here but to be honest this looks like far more complicated.

      but thats the way to go for such tasks.
      It's the nature of Android, that the app "stops" in the background.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Marek
        wrote on last edited by
        #3

        Probably yes, but I had impression that Android Service is not yet quite ready. In the discussion I mentioned someone asked 13 days ago if anyone had implemented Android Service along with Qt 5.7 app and that the sample project would be very useful.
        So the question is:
        can I get away with just the separate thread for GPS and network connectivity ?

        Best Regards
        Marek

        raven-worxR 1 Reply Last reply
        0
        • M Marek

          Probably yes, but I had impression that Android Service is not yet quite ready. In the discussion I mentioned someone asked 13 days ago if anyone had implemented Android Service along with Qt 5.7 app and that the sample project would be very useful.
          So the question is:
          can I get away with just the separate thread for GPS and network connectivity ?

          Best Regards
          Marek

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @Marek said in QApplication main loop doesn't work when in background on Android:

          can I get away with just the separate thread for GPS and network connectivity ?

          as i already said...no
          As soon as the app goes into the background you its undeterminated what happens. Your app might even get removed from memory when the OS needs to.
          Services are designed for background processing

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          M 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @Marek said in QApplication main loop doesn't work when in background on Android:

            can I get away with just the separate thread for GPS and network connectivity ?

            as i already said...no
            As soon as the app goes into the background you its undeterminated what happens. Your app might even get removed from memory when the OS needs to.
            Services are designed for background processing

            M Offline
            M Offline
            Marek
            wrote on last edited by
            #5

            @raven-worx said in QApplication main loop doesn't work when in background on Android:

            Your app might even get removed from memory when the OS needs to.

            This I know.
            However, google maps works in my phone when it is locked, not refreshing the screen but getting data from GPS it will be closed when system needs more resources.

            But if the Android Service is a way to go, is there a working example for the app using service ?

            And does iOS and Windows Mobile behave in the same way. I read somewhere that in Windows Mobile there shouldn't even be "exit" button in application, as it is system responsibility to close the app.

            Best Regards
            Marek

            raven-worxR 1 Reply Last reply
            0
            • M Marek

              @raven-worx said in QApplication main loop doesn't work when in background on Android:

              Your app might even get removed from memory when the OS needs to.

              This I know.
              However, google maps works in my phone when it is locked, not refreshing the screen but getting data from GPS it will be closed when system needs more resources.

              But if the Android Service is a way to go, is there a working example for the app using service ?

              And does iOS and Windows Mobile behave in the same way. I read somewhere that in Windows Mobile there shouldn't even be "exit" button in application, as it is system responsibility to close the app.

              Best Regards
              Marek

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @Marek said in QApplication main loop doesn't work when in background on Android:

              But if the Android Service is a way to go, is there a working example for the app using service ?

              See this and this.

              And does iOS and Windows Mobile behave in the same way

              Can't answer that, since i haven't done any work on these platforms yet.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Marek
                wrote on last edited by
                #7

                Hi

                Thanks for example it runs on my Android phone, and it is running in app settings when I close the app.

                I have some questions, forgive me my ignorance, I haven't worked with services yet.
                After reading this

                1. My app and service is in fact one application, signle .so file ? Part of which runs as a service and part as an application (activity)? In main.cpp there is:
                if (QCoreApplication::arguments().count() > 1){
                        qDebug() << "I am the service";
                    }
                    else{
                        qDebug() << "I am the application";
                

                I can see only

                source/cpp/main.cpp:19 (int main(int, char**)): I am the application
                

                Is this due to debugger not following service? So, in fact application is started twice? Once as the activity and then as the service?
                Should I put part of service code and activity code inside proper main.cpp part?

                1. "A service runs in the same process as the application in which it is declared and in the main thread of that application, by default" - so I assume that main thread is running even if main event-loop does not ? or QtService creates its own thread?

                2. I assume I need "Started" type service as it will run after app is closed by system, until I close service or it closes itself

                3. How to exchange data between service and activity? on Android page they say about IPC, but if it is single app...

                4. How to stop activity when user chooses to stop application?

                Best Regards
                Marek

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Marek
                  wrote on last edited by
                  #8

                  Hi all

                  Threads won't work for me, I had to learn this the hard way.
                  I have rewritten app using separate threads, made thread synchrnization, and it works, even when phone is locked, so great!
                  No, not really... main thread stops when app is in background, worker works, as long as, I have USB cable connected to the phone, when I disconnect the cable, worker thread continues to work for about 10-15 seconds and also goes to sleep, what a cruel experience.
                  I'm using Qt 5.7
                  Can someone give me more directions about Android Service inQt ?

                  Best Regards
                  Marek

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    This article is likely what you are looking for.

                    Hope it helps

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    1
                    • SGaistS SGaist

                      Hi,

                      This article is likely what you are looking for.

                      Hope it helps

                      M Offline
                      M Offline
                      Marek
                      wrote on last edited by
                      #10

                      Posted on September 28, 2016 by BogDan Vatra

                      wow, this is fresh!

                      Thanks, will look into this
                      Marek

                      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