Your foreground service can be of a different type, mine is a location foreground service
Override onStartCommand function
You can define an expression to handle the different actions tht are permitted within your service
In this case I have an actions enum defined with the service enum class Actions { START, STOP}
Create notifications and notification channels
Notification channels can be created in a seprate class and initialized on onCreate()
My notification includes Pending Intents
The first PendingIntent(Action that will take place at a later time) makes it so if the user clicks on the notification the MainActivity::class.java is started
PendingIntent.getActivity() ~ It is used to create a PendingIntent that will start an activity when triggered.
This happens because we pass it an Intent Intent(this, MainActivity::class.java) to start the MainActivity::class
PendingIntent.FLAG_IMMUTABLE This flag indicates that the PendingIntent should be immutable, meaning that its configuration cannot be changed after it is created.
The Second PendingIntent(Action that will take place at a later time) makes it so if the user clicks on the notification action button the service is stopped
PendingIntent.getService() ~ Similar to getActivity(), it is used to create a PendingIntent, but instead of starting an activity, it starts a service when triggered.
This happens because we pass it an Intent Intent(this, ForegroundService::class.java).apply {action = Actions.STOP.name} to start the ForegroundService::class.java
PendingIntent.FLAG_IMMUTABLE This flag indicates that the PendingIntent should be immutable, meaning that its configuration cannot be changed after it is created.
Recall onStartCommand() contains an expression that evaluates what is passed into the intent
Define your logic that is used when your service is started
Using a LocationManager and LocationListener
You have now defined a general skeleton for your foreground service
You created a means to handle intents to start your foreground service
You created your own notification
You created pending intents to go with your notification
Starts MainActivity::Class.java and stops the foreground service
You created logic that is executed when the service is started