I\'ve read just about every Stackoverflow answer that exists on this topic, but none of them worked.
Your service was killed by Doze or Standby mode of Android. That was introduced in Android 6.0 (API level 23).
Doze restrictions
The following restrictions apply to your apps while in Doze:
- Network access is suspended.
- The system ignores wake locks.
- Standard
AlarmManageralarms (includingsetExact()andsetWindow()) are deferred to the next maintenance window.- If you need to set alarms that fire while in Doze, use
setAndAllowWhileIdle()orsetExactAndAllowWhileIdle().- Alarms set with
setAlarmClock()continue to fire normally — the system exits Doze shortly before those alarms fire.- The system does not perform Wi-Fi scans.
- The system does not allow sync adapters to run. The system does not allow
JobSchedulerto run.
So system ignored your Alarm Clocks, Scheduler, etc.
To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.
Still if app need to run its service always, then we can create foreground service.
Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.
So create a foreground service. In which you will put a notification for user while your service is running. See this answer (There are many others)
Now what if you don't want a notification for your service. A solution is for that.
You can create some periodic task that will start your service, service will do its work and stops itself. By this your app will not be considered battery draining.
You can create periodic task with Alarm Manager, Job Scheduler, Evernote-Jobs or Work Manager.
I created forever running service with Work-Manager, that is working perfectly.