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. Keep Android Activity running, while not visible

Keep Android Activity running, while not visible

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
androidportablebackground conn
3 Posts 2 Posters 1.6k 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.
  • P Offline
    P Offline
    pogojotz
    wrote on 25 Mar 2019, 14:26 last edited by
    #1

    Hi,
    we have an application, which is originally targeting desktops and we ported it to Android 8, changing only the parts really needed, so we can stay with one common code base, using #ifdefs where needed.
    My problem is now, that the application is expected to send regular packets to a server and also actively listen on a TCP connection. This works fine on a desktop. On Android this works also fine as long as the app is in foreground and display is on (or the USB charger connected). Once the app goes into doze no network interaction takes place.
    I've read Sending keep-alive Messages on Android even if the device is in sleep/doze mode, where the OP seemed to have realized exactly, what I needed, by creating a foreground service and using that to keep the activity awake. I tried that and managed to create a foreground service, but just the existence of a bound foreground service does not seem to keep the activity awake and the event loop running.
    Then I read somewhere that the activity is kept in foreground, if the foreground service runs in the same process, but that didn't seem to help either. (Also I had to derive my service from android.app.Service directly, because that doesn't work with QtService and I don't understand the side effects).

    I don't know what I missed. All I need is grant execution resources to the activity from my service every few seconds. I know I could move the network connection stuff to the service, but that would mean a huge code change and introduce inconsistencies with other platforms.

    Do you have any ideas what I could be missing?

    J 1 Reply Last reply 26 Mar 2019, 06:34
    0
    • P pogojotz
      25 Mar 2019, 14:26

      Hi,
      we have an application, which is originally targeting desktops and we ported it to Android 8, changing only the parts really needed, so we can stay with one common code base, using #ifdefs where needed.
      My problem is now, that the application is expected to send regular packets to a server and also actively listen on a TCP connection. This works fine on a desktop. On Android this works also fine as long as the app is in foreground and display is on (or the USB charger connected). Once the app goes into doze no network interaction takes place.
      I've read Sending keep-alive Messages on Android even if the device is in sleep/doze mode, where the OP seemed to have realized exactly, what I needed, by creating a foreground service and using that to keep the activity awake. I tried that and managed to create a foreground service, but just the existence of a bound foreground service does not seem to keep the activity awake and the event loop running.
      Then I read somewhere that the activity is kept in foreground, if the foreground service runs in the same process, but that didn't seem to help either. (Also I had to derive my service from android.app.Service directly, because that doesn't work with QtService and I don't understand the side effects).

      I don't know what I missed. All I need is grant execution resources to the activity from my service every few seconds. I know I could move the network connection stuff to the service, but that would mean a huge code change and introduce inconsistencies with other platforms.

      Do you have any ideas what I could be missing?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 26 Mar 2019, 06:34 last edited by
      #2

      @pogojotz I think you need a background service: https://developer.android.com/guide/components/services

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

      P 1 Reply Last reply 26 Mar 2019, 10:04
      1
      • J jsulm
        26 Mar 2019, 06:34

        @pogojotz I think you need a background service: https://developer.android.com/guide/components/services

        P Offline
        P Offline
        pogojotz
        wrote on 26 Mar 2019, 10:04 last edited by
        #3

        @jsulm I have tried that and this didn't work. Background services can and will be suspended as soon as there is another foreground activity/service, which seems more important to the OS. This option was also discussed in the thread I linked above. A foreground service on the other hand stays active even if the bound activity is in background.

        Please also note, that I target Android 8, which imposes even stricter policies on background services, as described here.

        1 Reply Last reply
        0

        3/3

        26 Mar 2019, 10:04

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved