[TIMOB-26724] Android: How to achieve a service that can continue to run at regular intervals, even if the device goes to sleep.
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | None |
Status | Closed |
Resolution | Won't Do |
Resolution Date | 2019-03-18T18:07:25.000+0000 |
Affected Version/s | Release 7.5.0, Release 7.5.1 |
Fix Version/s | n/a |
Components | Android |
Labels | 2019-cl |
Reporter | Aminul Islam |
Assignee | Unknown |
Created | 2019-01-14T13:13:14.000+0000 |
Updated | 2019-03-18T18:07:39.000+0000 |
This is by Google's design. A foreground service's main purpose is to prevent the app from being terminated while backgrounded and to prevent sensor data from being "throttled" while backgrounded. It does not prevent the device from going to sleep. https://developer.android.com/about/versions/oreo/background https://developer.android.com/about/versions/oreo/background-location-limits The simplest solution is to set the window's "keepScreenOn" property to
true
. Just note that this will only work while that window is in the foreground. https://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.Window-property-keepScreenOn Alternatively, you can use a wake-lock like you've said. This is the only means of preventing the device from going to sleep while your app is backgrounded. Just be aware that using a wake-lock perpetually in the background is considered "naughty" and will kill battery life. You should only use it if absolutely necessary. (Notice that Google has been going to great lengths to improve battery life and they're the ones who frown on this behavior.) Titanium's posted notifications (including foreground service notifications) will automatically apply a 3 second wake-lock if the permission is set in your "tiapp.xml" file as follows.Unfortunately, Titanium does not allow you to directly control the wake-lock (ie: acquire/release). So, while our notification API allows you to increase the wake-lock time higher than 3 seconds, the problem is you don't have a means of releasing the wake-lock when you're done with it (removing the notification won't release the wake-lock). So, what you can do instead is "update" the foreground notification using the same notification ID based on a timer (perhaps the service's timer?) to keep the wake-lock on. That is, call the
foregroundNotify()
function again with the same ID. Updating a notification won't cause it to pop up at the top of the screen, however, it will still make a sound when updated so I recommend that you lower the priority and disable the sound.