‫Welcome back.
‫In this video, we'll deep dive into services and their types.
‫A service is a type of Android component that runs in the background independently of the user interface.
‫It's designed for tasks that don't require direct user interaction but need to perform some operation
‫for an extended period.
‫Services are commonly used to handle tasks like background music, playback, network operations, data
‫processing and more.
‫There are three types of services in Android the foreground, background and bound services.
‫The foreground services are noticeable to the user and are used for tasks that require ongoing user
‫interaction or notification.
‫They display a persistent notification in the notification bar to inform the user that the service is
‫running.
‫For example, a music player app uses a foreground service to play music, even when the app is not
‫in the foreground.
‫The notification could show the current song, provide playback controls and allow the user to interact
‫with the music.
‫The background services perform tasks that are not directly visible to the user and don't require ongoing
‫notification.
‫They are more suitable for tasks that can run silently without requiring constant user attention.
‫For example, an app that periodically syncs data with a server might use a background service to perform
‫these sync operations at specific intervals, while the bound services in Android allow the components
‫of the application like activity to bound themselves with them, Bound services perform their tasks
‫as long as any application component is bound to it.
‫More than one component is allowed to bind themselves with a service at a time in order to bind an application
‫component with a service bind Service method is used in Android.
‫Services have a lifecycle that defines their creation, operation and destruction.
‫There are two types of services bound and unbound services.
‫Let's explore the lifecycle of both types of services.
‫Let's start with the Unbound service.
‫Unbound services are started using the Start service method and operate independently of components
‫that started them.
‫They can continue running even if the component that started them.
‫For example, an activity is destroyed.
‫Then the Oncreate method is called.
‫This method is called when the service is initially created.
‫It's used for one time initialization tasks.
‫The Onstart Command Method.
‫This method is called when the service is starting.
‫Using the Start service method, it provides the intent path to the start service method and other parameters.
‫You perform background tasks.
‫In this method, you can return values like start sticky or start not sticky to indicate how the system
‫should handle the service.
‫If it is killed due to the resource constraints, then the service is stopped by itself or a client
‫and the service is stopped.
‫So no callback the Ondestroy method.
‫This method is called when the service is about to be destroyed.
‫You should release any resources you've acquired and perform cleanup tasks.
‫The second type of services is the bound services.
‫The bound services are usually used to establish a connection between components, activities and the
‫service.
‫They are started using the Bind service method and are closely tied to the components that bound to
‫them.
‫When all components unbind from the service, the service is automatically destroyed.
‫The Oncreate method as in unbound services.
‫This method is called when the service is initially created.
‫The unbind method and bind method is called when a component binds to the service.
‫Using the online service method, it returns an eye binder interface that the client component can use
‫to interact with the service.
‫The service can perform, set up and provide the client with this binder.
‫This allows the client to interact with the service and all clients and bind by calling unbind.
‫Service method on unbind method is called when all components have unbound from the service.
‫You might return it through here if you want the service to continue running even no more components
‫are bound, but typically returning false leads to the service being destroyed when unbound on rebind
‫method.
‫This method is called when a component that was already bound to the service Rebinds using the bind
‫service method and the Ondestroy method similar to unbound services.
‫This method is called when the service is about to be destroyed.
‫Perform cleanup tasks here.
‫To summarize, the unbound services can run independently of components and continue running even if
‫the starting component is destroyed.
‫Bound services are more tightly connected to the components that bind to them.
‫They are automatically destroyed when all bound components unbind from them.
‫Be cautious about managing the service lifecycle properly.
‫If you don't manage it correctly, it can lead to resource leaks or unintended behavior in your application.
‫And remember that the specific use case for each type of service will determine whether to use bound
‫or unbound services and how to handle their lifecycle.