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. Android service stops working after 1 minute! when device is not connected to power supply , but still seen in the services section of the device
Qt 6.11 is out! See what's new in the release blog

Android service stops working after 1 minute! when device is not connected to power supply , but still seen in the services section of the device

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
13 Posts 2 Posters 8.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.
  • M Offline
    M Offline
    Mena
    wrote on last edited by Mena
    #1

    I created an android service that issues a push-notification every 5 minutes using QTimer.
    I noticed that when the service starts it has a memory consumption of around 14MB.
    Sometimes, it continues to work perfectly for hours.

    But, usually it stops functioning after exactly 1 minute. I also noticed that it stays visible in the services section of the device however its memory consumption drops to 5.6MB after 1 minute. That's when it stops working.
    Why is this happening?
    Tell me if you want me to post some code.

    N.B: I figured out when that happens. Only when the device is not connected to power supply. ie. not charging via USB.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Schluchti
      wrote on last edited by
      #2

      Does that only happen when you are debugging your application (i.e your phone is connected to your PC)? If so, you could check your developer options on your phone. Maybe there is a power saving option or something like that, that kicks in after one minute.

      Have you looked at the output of adb logcat? Is there something suspicious in there?

      Want to read more about Qt?

      https://gympulsr.com/blog/qt/

      Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

      M 1 Reply Last reply
      0
      • S Schluchti

        Does that only happen when you are debugging your application (i.e your phone is connected to your PC)? If so, you could check your developer options on your phone. Maybe there is a power saving option or something like that, that kicks in after one minute.

        Have you looked at the output of adb logcat? Is there something suspicious in there?

        M Offline
        M Offline
        Mena
        wrote on last edited by
        #3

        @Schluchti It is the other way. Only when the USB is not connected. And since it is not connected, i cannot check logcat.

        S 1 Reply Last reply
        0
        • M Mena

          @Schluchti It is the other way. Only when the USB is not connected. And since it is not connected, i cannot check logcat.

          S Offline
          S Offline
          Schluchti
          wrote on last edited by
          #4

          @Mena As far as I know, Android is allowed to kill services when it needs system resources. So before you start your application I would make sure that there aren't many applications running in the background (just to be sure that the application won't get killed due to lack of resources).

          You could try to connect to your phone after your application has stopped working to run adb logcat. logcat usually uses a ringbuffer to store the logs, so if you are lucky (and your buffer is large enough) you can still see something. On some phones you even can configure the size of the ringbuffer in the developer settings of your phone.

          Want to read more about Qt?

          https://gympulsr.com/blog/qt/

          Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

          M 1 Reply Last reply
          0
          • S Schluchti

            @Mena As far as I know, Android is allowed to kill services when it needs system resources. So before you start your application I would make sure that there aren't many applications running in the background (just to be sure that the application won't get killed due to lack of resources).

            You could try to connect to your phone after your application has stopped working to run adb logcat. logcat usually uses a ringbuffer to store the logs, so if you are lucky (and your buffer is large enough) you can still see something. On some phones you even can configure the size of the ringbuffer in the developer settings of your phone.

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

            @Schluchti the service is not killed, but rather freezes during the time the screen is Off + USB not connected.

            As soon as i turn on the screen it works again immediately.

            I think it has something to do with this:
            https://developer.android.com/training/scheduling/wakelock.html

            But, I don't know yet, still searching....
            Couldn't find smth in Logcat.

            S 1 Reply Last reply
            0
            • M Mena

              @Schluchti the service is not killed, but rather freezes during the time the screen is Off + USB not connected.

              As soon as i turn on the screen it works again immediately.

              I think it has something to do with this:
              https://developer.android.com/training/scheduling/wakelock.html

              But, I don't know yet, still searching....
              Couldn't find smth in Logcat.

              S Offline
              S Offline
              Schluchti
              wrote on last edited by
              #6

              @Mena That's possible. What do you want to do in the service? Does the service need to be running infinitely or do you just want to do a small task in the background?

              There is also the possibility to run the service in the foreground. I did this for one of my applications to show the steps a user has walked in the notification bar. The disadvantage of this approach is, that it obviously drains the battery more and that the user always sees your application in the notification bar.

              Want to read more about Qt?

              https://gympulsr.com/blog/qt/

              Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

              M 2 Replies Last reply
              0
              • S Schluchti

                @Mena That's possible. What do you want to do in the service? Does the service need to be running infinitely or do you just want to do a small task in the background?

                There is also the possibility to run the service in the foreground. I did this for one of my applications to show the steps a user has walked in the notification bar. The disadvantage of this approach is, that it obviously drains the battery more and that the user always sees your application in the notification bar.

                M Offline
                M Offline
                Mena
                wrote on last edited by
                #7

                @Schluchti The service is an alarm clock. That issues a push notification when the current time is equal to the alarm time set by the user. So, I need the service to be on all the time. At least for now till I optimize the algorithm.

                S 1 Reply Last reply
                0
                • S Schluchti

                  @Mena That's possible. What do you want to do in the service? Does the service need to be running infinitely or do you just want to do a small task in the background?

                  There is also the possibility to run the service in the foreground. I did this for one of my applications to show the steps a user has walked in the notification bar. The disadvantage of this approach is, that it obviously drains the battery more and that the user always sees your application in the notification bar.

                  M Offline
                  M Offline
                  Mena
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • M Mena

                    @Schluchti The service is an alarm clock. That issues a push notification when the current time is equal to the alarm time set by the user. So, I need the service to be on all the time. At least for now till I optimize the algorithm.

                    S Offline
                    S Offline
                    Schluchti
                    wrote on last edited by
                    #9

                    @Mena So you actually only need to run the service when the alarm fires, right? Have you considered using a AlarmManager, which starts the service at the given time? [1] In that case you could probably also consider starting the service as foreground service, because when the alarm goes off you usually want the user to react to the alarm (i.e turn alarm off).

                    [1] https://stackoverflow.com/questions/8321443/how-to-start-service-using-alarm-manager-in-android

                    Want to read more about Qt?

                    https://gympulsr.com/blog/qt/

                    Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                    M 1 Reply Last reply
                    0
                    • S Schluchti

                      @Mena So you actually only need to run the service when the alarm fires, right? Have you considered using a AlarmManager, which starts the service at the given time? [1] In that case you could probably also consider starting the service as foreground service, because when the alarm goes off you usually want the user to react to the alarm (i.e turn alarm off).

                      [1] https://stackoverflow.com/questions/8321443/how-to-start-service-using-alarm-manager-in-android

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

                      @Schluchti No, the service is the process that does the comparison in the background between the current time and the alarm time set by the user. If equal to each other, it fires a push-notification.

                      S 1 Reply Last reply
                      0
                      • M Mena

                        @Schluchti No, the service is the process that does the comparison in the background between the current time and the alarm time set by the user. If equal to each other, it fires a push-notification.

                        S Offline
                        S Offline
                        Schluchti
                        wrote on last edited by Schluchti
                        #11

                        @Mena Sorry, I think I don't get it, so maybe this is a stupid question, but why do you need to constantly compare the current time with the alarm time in the service? Can't you just calculate the offset (seconds until the alarm will fire) and use the AlarmManager?

                        e.q: User creates a new alarm at 02:00 pm and wants the alarm to go off at 08:00 pm. The difference in seconds is now 6 hrs (or 21600 seconds). So you could use the AlarmManager to trigger a alarm in 21600 secons which starts your (foreground)service. When the service starts you create your push-notification.

                        Want to read more about Qt?

                        https://gympulsr.com/blog/qt/

                        Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                        M 1 Reply Last reply
                        0
                        • S Schluchti

                          @Mena Sorry, I think I don't get it, so maybe this is a stupid question, but why do you need to constantly compare the current time with the alarm time in the service? Can't you just calculate the offset (seconds until the alarm will fire) and use the AlarmManager?

                          e.q: User creates a new alarm at 02:00 pm and wants the alarm to go off at 08:00 pm. The difference in seconds is now 6 hrs (or 21600 seconds). So you could use the AlarmManager to trigger a alarm in 21600 secons which starts your (foreground)service. When the service starts you create your push-notification.

                          M Offline
                          M Offline
                          Mena
                          wrote on last edited by
                          #12

                          @Schluchti Well yes of course this is possible. You have a valid point!
                          But, maybe in the future for some reason I would need to do something else this way. So, I just want to know the solution to such problem if there is one, So I would know if I should always avoid that route.
                          I just want to know what the problem is? Sorry for being stubborn :D

                          I am currently trying this:
                          https://stackoverflow.com/questions/6091270/how-can-i-keep-my-android-service-running-when-the-screen-is-turned-off

                          and this:
                          https://stackoverflow.com/questions/5280951/android-sound-gets-disabled-after-sometime

                          S 1 Reply Last reply
                          0
                          • M Mena

                            @Schluchti Well yes of course this is possible. You have a valid point!
                            But, maybe in the future for some reason I would need to do something else this way. So, I just want to know the solution to such problem if there is one, So I would know if I should always avoid that route.
                            I just want to know what the problem is? Sorry for being stubborn :D

                            I am currently trying this:
                            https://stackoverflow.com/questions/6091270/how-can-i-keep-my-android-service-running-when-the-screen-is-turned-off

                            and this:
                            https://stackoverflow.com/questions/5280951/android-sound-gets-disabled-after-sometime

                            S Offline
                            S Offline
                            Schluchti
                            wrote on last edited by
                            #13

                            @Mena I see ;-)

                            Looks like you are not the only one who has this problem: https://bugreports.qt.io/browse/QTBUG-56648

                            Want to read more about Qt?

                            https://gympulsr.com/blog/qt/

                            Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

                            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