‫Welcome back.
‫To better understand the services in Android, let's move to Android Studio.
‫I created this project, the services app, and it's very simple application that contains one text
‫view as a title and two buttons, one for playing the default ringtone with ID play ringtone, and one
‫for stopping the default ringtone with ID stop ranked.
‫Very simple app two buttons.
‫Now I want from you to focus with me because I'm going to create the service class here.
‫Inside this package, go and create a new Kotlin class and I'll name it as my service.
‫And to tell Android Studio that this class will act as a service class, we need to extend the from
‫service class Alt+ enter to import the correct class and make sure that you import the service from
‫Android app.
‫Please guys, pay attention not the service in provider and not the service in Bluetooth class, just
‫the service in Android app and make sure that you import the correct package.
‫Okay Alt+ enter to implement the members and here we need to implement the unbind method.
‫This is the constructor of the service class.
‫When you extend a class in Kotlin, you need to call the constructor of the superclass, and in this
‫case the service class is the superclass.
‫The service class provides the fundamental structure and methods for creating a service in Android,
‫as service in Android is a component that runs in the background and doesn't have a user interface.
‫It can perform long running operations or handle tasks that need to continue, even when the app is
‫not in the foreground.
‫Common use cases for services include playing music, fetching data from the internet, or performing
‫periodic tasks.
‫In this example, we're going to play the default ringtone of the device in the background using this
‫service class.
‫The unbind method is part of the Android service lifecycle, and is used when the service allows other
‫components, such as activities, to bind to it.
‫When a component binds to a service, it establishes a connection that allows the component to interact
‫with the service, invoke methods, and receive data.
‫The method returns a I binder object or NULL if binding is not allowed.
‫If NULL is returned, it indicates that the service doesn't support binding, and in this case I don't
‫need to include binding, so I'll use return null.
‫The I binder is an interface that defines the programming interface that clients can use to interact
‫with the service.
‫When a component, such as an activity, wants to bind to a service, it typically uses the bind service
‫method, passing an intent that identifies the service and the service connection callback.
‫The service connection callbacks and the service connected method is then called, providing the I binder
‫object returned by the unbind method.
‫But I'll keep it very simple.
‫I don't want to make it complicated and complex, so I'll return null because I'm not going to bind
‫anything to this service.
‫I need to control the default and playing the ringtone the default ringtone.
‫Using this service I want from you to focus with me.
‫I'm not going to use the unbind method.
‫I'll create an instance of media player that play any media file.
‫Here I'll create late initiate var media player and I'll use this instance from Android Media.
‫Under the unbind method, I'll override a new method called on start command.
‫The on start command method is a crucial part of the Android service lifecycle.
‫It's called when a client, such as an activity or another service, starts the service using the start
‫service method.
‫Later on in the main activity, I'll use Start service method to execute this method.
‫The intent parameter carries information that might be passed to the service when it is started.
‫This can include data or instructions provided by the component that started the service int, which
‫is the flags this parameter.
‫Provides additional information about how the service was started.
‫It's a bitmask that can be used to extract various flags.
‫For example, you can check if the service was started with start flag read delivery or start flag retry
‫or other forms.
‫The start ID, which is the third parameter of the on start command method.
‫This is an identifier for the specific instance of the service.
‫Each time the service is started, this ID is incremented.
‫It can be used to identify and manage the different Start requests.
‫Here we need to specify a return type for this function because it specifies an int as a returning type.
‫When start command method is an integer that represents the service's behavior.
‫Regarding ING restarts, there are common return values including start sticky.
‫For example, return start sticky, the service is restarted if it gets terminated and the system will
‫recreate it, and I'll use this return value.
‫Also, you can use start not sticky.
‫The service is not restarted and the system doesn't recreate it.
‫You can use start or start redeliver intent similar to start sticky, but it also read delivers the
‫last intent if the service is terminated.
‫Here I'll use start stick.
‫This indicates that the service should be restarted by the system if it gets terminated, and it should
‫try to redeliver the last intent that was used to start the service inside the on start command method,
‫you would typically include the logic for handling the start command.
‫This might involve initializing resources, performing background tasks, or responding to the instructions
‫provided through the intent.
‫In the next video, we're going to add the logic to be executed when the service is started, and we're
‫going to test our application.