0

Our company is developing an android application that uses network communication to send GPS signals from devices. The devices are the same and they are all work tools, so we do not have to worry about battery draining, or etc. Currently the activity has a thread, which communicates with the server. The problem is that when the device is locked and it goes to sleep, the network communication breaks.

I've tried to put a partial wake lock to the onPause method to keep the CPU on, and release the wakelock in the onResume method, but it seems not to work. Any idea how to prevent the sleep, or keep flowless communication between the client and the server?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Bandi Tóth
  • 167
  • 1
  • 9
  • Forget to mention: When the device is on charger, the problem is not occures. – Bandi Tóth Feb 22 '18 at 13:55
  • Why dont you make the task you have on a thread run through an Android Service? https://developer.android.com/guide/components/services.html – iamIcarus Feb 22 '18 at 13:57
  • 4
    "so we do not have to worry about battery draining" -- yes, you do. "The problem is that, when the device is locked, and it goes to sleep, the network communication breaks" -- if you are communicating via WiFi, you would need a `WifiLock` as well as a `WakeLock`. "'ve tried to put a partial wake lock to the onPause method, to keep the CPU on, and release the wakelock in the onResume method, but it seems not to work" -- without a [mcve] and a detailed explanation of "it seems not to work", it will be difficult for anyone to help you. – CommonsWare Feb 22 '18 at 14:01
  • You need to implement foreground service https://developer.android.com/guide/components/services.html#Foreground – egoldx Feb 22 '18 at 14:03

1 Answers1

0

Unfortunately, it is the new behavior, You can read here: https://developer.android.com/about/versions/nougat/android-7.0-changes.html See the Doze section.

When a device is on battery power, and the screen has been off for a certain time, the device enters Doze and applies the first subset of restrictions: It shuts off app network access, and defers jobs and syncs. If the device is stationary for a certain time after entering Doze, the system applies the rest of the Doze restrictions to PowerManager.WakeLock, AlarmManager alarms, GPS, and Wi-Fi scans.

It can be solved by using Foreground Service like mentioned above in comments by @egoldx.

It is surely a bad practice to try holding a wakelock, even partial, all the time.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Pavel B.
  • 805
  • 10
  • 13