An Android Service, is an application component that can perform long-running operations in the background, and it doesn’t provide a user interface.
A few example uses for a service could be:
- Network transactions
- Play music
- Perform file I/O operations
- Interact with content providers
- Background operations
I’ve recently implemented a service in one of my applications to start a sound triggered by a BroadcastReceiver when an alarm expires and here is how I implemented it with Kotlin.
Then, this is how it’s started from the BroadcastReceiver.
Finally don’t forget to add your reference to your app’s manifest file.
By using this service I’m able to stop it from anywhere within the application and stop the alarm sound when the user interacts with my app by calling stopService().