Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26724] Android: How to achieve a service that can continue to run at regular intervals, even if the device goes to sleep.

GitHub Issuen/a
TypeNew Feature
PriorityNone
StatusClosed
ResolutionWon't Do
Resolution Date2019-03-18T18:07:25.000+0000
Affected Version/sRelease 7.5.0, Release 7.5.1
Fix Version/sn/a
ComponentsAndroid
Labels2019-cl
ReporterAminul Islam
AssigneeUnknown
Created2019-01-14T13:13:14.000+0000
Updated2019-03-18T18:07:39.000+0000

Description

Comments

  1. Joshua Quick 2019-01-14

    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.
       <?xml version="1.0" encoding="UTF-8"?>
       <ti:app xmlns:ti="http://ti.appcelerator.org">
       	<android xmlns:android="http://schemas.android.com/apk/res/android">
       		<manifest>
       			<uses-permission android:name="android.permission.WAKE_LOCK"/>
       		</manifest>
       	</android>
       </ti:app>
       
    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.

JSON Source