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. Sending keep-alive Messages on Android even if the device is in sleep/doze mode
Forum Updated to NodeBB v4.3 + New Features

Sending keep-alive Messages on Android even if the device is in sleep/doze mode

Scheduled Pinned Locked Moved Solved Mobile and Embedded
18 Posts 5 Posters 8.7k Views 2 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.
  • W Wowalive
    5 Jun 2018, 06:42

    Okay thanks. So I need a native background service / method for sending, receiving and processing data? Did I understand it correctly?

    @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

    May i ask what security reasons exactly preventing you from using push notifications?

    I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

    Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

    J Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 5 Jun 2018, 06:52 last edited by J.Hilk 6 May 2018, 09:40
    #8

    @Wowalive said in [Sending keep-alive Messages on Android even if the device is in sleep/doze mode]

    Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

    constant GPS usage, Background data exchange and computation, your phone battery will be empty in a matter of very few hours!

    Also I hope this is not intended to be a cross plattform application? Because its almost guaranteed, that the app would be rejected before even getting a background service permission from Apple.


    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.

    1 Reply Last reply
    2
    • W Offline
      W Offline
      Wowalive
      wrote on 5 Jun 2018, 10:59 last edited by
      #9

      Yes I know, but battery discharge isn't a problem for me because the devices are connected to charging stations as long as they are not in use.

      R 1 Reply Last reply 6 Jun 2018, 06:13
      0
      • W Wowalive
        5 Jun 2018, 06:42

        Okay thanks. So I need a native background service / method for sending, receiving and processing data? Did I understand it correctly?

        @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

        May i ask what security reasons exactly preventing you from using push notifications?

        I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

        Beyond that I have to send the gps position of the mobile device to the webservice as a payload of the alive-message every minute.

        A Offline
        A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on 5 Jun 2018, 20:20 last edited by
        #10

        @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

        I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

        Would it be possible to send a push notification "new data is available", which in turn causes your app to request the data on a secure channel?

        That would avoid the constant polling from phone side, and as the push notification just contains an info it should not be that critical.

        For me it sounds like a balanced compromise. Am I missing something?

        Qt has to stay free or it will die.

        W 1 Reply Last reply 6 Jun 2018, 07:20
        2
        • W Wowalive
          5 Jun 2018, 10:59

          Yes I know, but battery discharge isn't a problem for me because the devices are connected to charging stations as long as they are not in use.

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 6 Jun 2018, 06:13 last edited by
          #11

          @Wowalive
          using a WebSocket approach would be even better.
          The server just sends the data when there is something to send.

          --- 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
          • A aha_1980
            5 Jun 2018, 20:20

            @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

            I'm working with sensitive customer data and one of the requirements is not to use privacy critical servers like googles fcm / gcm. I do not know if there is even a possibility to use push notification without accessing these servers.

            Would it be possible to send a push notification "new data is available", which in turn causes your app to request the data on a secure channel?

            That would avoid the constant polling from phone side, and as the push notification just contains an info it should not be that critical.

            For me it sounds like a balanced compromise. Am I missing something?

            W Offline
            W Offline
            Wowalive
            wrote on 6 Jun 2018, 07:20 last edited by
            #12

            @aha_1980

            Yes I've thought about this approach too. It's a good compromise for updating the data in a secure and efficient way.

            But what about updating the gps position in a given cycle? It's necessary for me, that the server is informed about the position and the connectivity state (online/offline) of the mobile device every minute or so.

            Like I already said, battery drain isn't a problem for me though.

            R 1 Reply Last reply 6 Jun 2018, 09:18
            0
            • W Wowalive
              6 Jun 2018, 07:20

              @aha_1980

              Yes I've thought about this approach too. It's a good compromise for updating the data in a secure and efficient way.

              But what about updating the gps position in a given cycle? It's necessary for me, that the server is informed about the position and the connectivity state (online/offline) of the mobile device every minute or so.

              Like I already said, battery drain isn't a problem for me though.

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 6 Jun 2018, 09:18 last edited by raven-worx 6 Jun 2018, 09:21
              #13

              @Wowalive
              the big advantage of a WebSocket is that only the really needed data is transferred.
              There is only a single HTTP connection kept open. No HTTP connection negotiation data overhead for each poll request etc.
              One possibility could be if the connection is closed you can assume that the device is disconnected.

              With the linke i've posted in my first post you can implement the service with Qt. For the service <-> app communication you can use any IPC mechanismn (e.g. QtRemoteObjects to connect to a signal emitted from your service)

              --- 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
              • W Offline
                W Offline
                Wowalive
                wrote on 8 Jun 2018, 11:39 last edited by
                #14

                I created a Service, following this tutorial. The example from @BogDan-Vatra does not work for me (Program client has been terminated).

                The example from bbernhard (https://github.com/bbernhard/qtandroidservices_example) works for me. The service starts and keeps running, but somehow I cant call any Methods from service class. I suspect that the onCreate or onStartCommand method from the service will never be called.

                Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                R 1 Reply Last reply 8 Jun 2018, 11:56
                0
                • W Wowalive
                  8 Jun 2018, 11:39

                  I created a Service, following this tutorial. The example from @BogDan-Vatra does not work for me (Program client has been terminated).

                  The example from bbernhard (https://github.com/bbernhard/qtandroidservices_example) works for me. The service starts and keeps running, but somehow I cant call any Methods from service class. I suspect that the onCreate or onStartCommand method from the service will never be called.

                  Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 8 Jun 2018, 11:56 last edited by
                  #15

                  @Wowalive said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                  Isn't there just a service or method which pretends the main qt app from getting into app standby? So the app will keep running, even if the phone is locked?

                  @raven-worx said in Sending keep-alive Messages on Android even if the device is in sleep/doze mode:

                  This is a normal App lifecycle.

                  This is how the Android OS works. The OS decides when the app is closed to free up resources.

                  --- 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
                  4
                  • W Offline
                    W Offline
                    Wowalive
                    wrote on 11 Jun 2018, 09:14 last edited by
                    #16

                    Okay thank you. Then I will try to add an service which sends the network request.

                    Do you know, if I need a service for all the needed functions? Sending the request, receiving it and processing the data? Or will just the network request wake the app up, so my Qt code can proceed the processing?

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      Wowalive
                      wrote on 13 Jun 2018, 09:13 last edited by Wowalive
                      #17

                      Finally got it..
                      I've experimented a lot using services in relation to my problem. Noting really helped, because of the doze mode and app standby introduced in API 23.

                      The following restrictions apply to your apps while in Doze:

                      • Network access is suspended.
                      • The system ignores wake locks.
                      • Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window.
                        • If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle().
                        • Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.
                      • The system does not perform Wi-Fi scans.
                      • The system does not allow sync adapters to run.
                      • The system does not allow JobScheduler to run.

                      In API 24 and 26 further restrictions, like limited implicit broadcasts and limited gps background behavior, are added.

                      Firstly I tried to use a service with a native timer and asynctask for requesting data from a server, but the network connection will cut off after a couple of minutes and app tasks will be deferred to the maintenance window of doze. This affects the app even if I whitelist the app in battery optimization and enable partial wake locks to keep the cpu awake (see https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases).

                      With the AlarmManager (using setAndAllowWhileIdle() or setExactAndAllowWhileIdle()) you are able to set alarms that fire methods even if the device is in doze mode , but you cant fire alarms more than once per 9 minutes, per app. So for me this wasn't a solution, because I need to request every minute.

                      So referring to the background processing overview and the app standby exceptions I decided to implement a foreground service.
                      0_1528880606980_android-backgroundoptimazion.png

                      This solution was exactly what I was looking for. Now everything works fine. My Qt main thread does all the processing, and a simple foreground service holds the application awake. If you use that, pay attention to the high battery consumption.

                      1 Reply Last reply
                      2
                      • P Offline
                        P Offline
                        pogojotz
                        wrote on 22 Mar 2019, 15:00 last edited by
                        #18

                        @Wowalive and others: Thank you for providing this informative thread and sorry for necro bumping.

                        I have a similar task ahead and am trying your approach. So far I managed to create a foreground service, but my activity still gets suspended as soon as the device display is off. At least the permanent TCP connection to the server cuts off.

                        @Wowalive: How did you manage to keep your activity awake?

                        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