I have an android foreground service, called with the notification. In the service Im just logging every 10 seconds "Tick tack", but the priority of the service is navigate in a webview every X seconds, so Im using new threads and working in the main thread too.
If I have the app connected to the usb the logs seems ok, the "tick tack" is called every 10 seconds, same if the mobile is unlocked and Im wathing the logs on the app.
But when I disconnect the usb, or I lock the device this is what is happening:
2018-11-14 12:11:53.115 12596-12596/? I/Service: tick tack
2018-11-14 12:12:18.704 12596-12596/? I/Service: tick tack
2018-11-14 12:15:42.572 12596-12596/? I/Service: tick tack
2018-11-14 12:17:30.942 12596-12596/? I/Service: tick tack
2018-11-14 12:17:53.534 12596-12596/? I/Service: tick tack
2018-11-14 12:18:27.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:37.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:47.118 12596-12596/? I/Service: tick tack
2018-11-14 12:18:57.121 12596-12596/? I/Service: tick tack
2018-11-14 12:19:25.208 12596-12596/? I/Service: tick tack
2018-11-14 12:19:48.294 12596-12596/? I/Service: tick tack
What is the limit of foreground services? Can I do hard foreground work even the device is idle?
I have made this library the other day for something similar service in background and foreground
And its working absolutely fine with no problems.
The reason I choose to run it with
AlarmManager
is that theAlarmManager
is a great candidate for scheduling if an application needs to perform a local event + allows an application to schedule tasks that may need to run or repeat beyond the scope of its lifecycle. This allows the application to perform some function even after the application process or all of its Android components have been cleaned up by the system.UPDATE
Call this method to start the service
Create
ServiceReciever
Classinside your
manifest
And thats should be it, tho if you like to run it in
Thread
Than you can add the next linesAnd to call this
class
editServiceReciever
to be like this