‫Welcome back.
‫The Onstart 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.
‫Inside the Onstart command method, you would typically include the logic for handling the start command.
‫This might involve initializing resources, performing background tasks, or responding to instructions
‫provided through the intent.
‫As mentioned in the previous video, this on start command has three parameters.
‫The first one is the intent.
‫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 flags, which
‫is of type int.
‫This parameter provides additional information about how the service was started.
‫It is a Bitmask that can be used to extract various flags.
‫For example, you can check if the service was started with start flag Redelivery or Start flag retry
‫or other flags.
‫The start ID which is an int.
‫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.
‫Now let's add the logic for our own start command.
‫Here I'll start with Mediaplayer equals to Mediaplayer dot create.
‫Here I'm going to initialize and create an instance of the media player that is responsible for playing
‫the audio of the default ringtone of the device.
‫The first parameter is this, which is the context, and the second parameter is the audio file that
‫we are going to play.
‫And to access the default system ringtone, we need to use settings, which is this class Alt+ enter
‫to import it and make sure that you import settings class from Android dot provider settings dot system
‫dot default ringtone Uri.
‫Here I am accessing the default ringtone of the device.
‫Now we need to play the ringtone audio on loop which is continuously so media player dot set looping
‫or is looping.
‫Here I'm looping it to make it continuously and to start the media player I'll call media player dot
‫start function.
‫This is the logic that we need to add inside the on start command method.
‫Here we create and initialize the media player.
‫Here I am setting the media player to repeat ringtone media player dot start and return start sticky.
‫Start sticky is the service return type, which is of type int that 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.
‫I need to override another method, which is Ondestroy and Ondestroy.
‫We talked about this callback method in the Android lifecycle method.
‫Here I'm calling it to force the media player to stop when we destroy the application.
‫Let's move to the main activity.
‫I'll initialize the widgets, which are two buttons.
‫Let's handle the click events on these buttons.
‫Set on Click Listener.
‫And here I need to start the service.
‫I'll start with var start intent equals to intent.
‫And here I need to pass the parameters the application context.
‫And here I need to specify the custom service my service act file.
‫This is the class that I should start.
‫This is the service that I should start.
‫So here my service dot class dot Java.
‫Here I am referring and telling Android to start the service.
‫My service when using intents.
‫An intent is a messaging object used to request an action from another component of the Android system,
‫such as starting a service.
‫The intent constructor is called with two parameters.
‫The first parameter is application context used to obtain the application context, and the second parameter
‫is the class of the service to be started, in this case my service class.
‫Then I need to call the start service method and I need to pass start service and this method start
‫service starts the service specified by the intent.
‫The start service method is used to initiate the execution of a service.
‫It doesn't bind to the service.
‫Instead, it sends a command to the service to perform some operation.
‫Here I am passing start service intent to the service to execute it.
‫And as mentioned before, we pass an intent here as a first parameter to execute this my start service
‫and call this function on start command.
‫So whenever you call the start service and pass the intent, the on start command method will be executed
‫and execute the logic inside it.
‫The last step is to add inside the manifest and inside the application tag the service tag.
‫So every service, every component in Android you should create an Android manifest tag for that.
‫If for example, if you are creating an activity, you should create an activity tag.
‫If you are creating a service like this, we need to create a service tag and specify it here in our
‫androidmanifest.xml file.
‫Don't miss whenever you create any service to add the service tag and you specify your service here
‫in the Androidmanifest Android manifest file, run the application again.
‫Guys, if you encountered this error, I told you how to solve it.
‫And please refer to the first section on how to fix Android Studio errors.
‫Change it to 34 and here to 34.
‫Sync now and run again.