Understanding and Using Services in Android: Background & Foreground Services

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video i'll show you how to utilize background and foreground services in android there are three types of services in android background foreground and bound each of these terms are misleading because it is not describing the behavior of how each servers are used this is describing how they are terminated for android developers a service is a component that runs on the background to perform a long-running task a background service is a service that runs only when the app is running so it will get terminated when the app is terminated a foreground service is a service that stays alive even when the app is terminated a bound service is a service that runs only if the component it is bound to is still alive let's see how we can create a background service if you have not already give the video a like share and subscribe to support the channel to create a background service create a new class and have it extend the service class inside the class override the on bind and onstar command methods the onstar command method is called every time you start the service either by calling start service or start foreground service this is where we want to define what the service will do for now we'll create a thread that prints a message to the terminal every two seconds now that we have our background service we need to let the app know about this service go to the manifest file inside the application element add a service element and use the name attribute to add the service to the app the next step is to start the service go to the main activity file in the oncreate method create an intent for the service call start surveys and pass in the intent if we run the app and look at the terminal a message will be displayed every two seconds if we minimize the app the service will continue to run if we terminate the app it will stop now let's see how we can create a foreground service create a new class and have it extend the service class overriding on bind and all stock command methods inside the onstar command method copy the code from the background service and paste it inside go to the manifest file and add the service to the app in order for us to use foreground services we need to add the foreground service permission in the manifest file to enable it now we need to start the service go to the main activity file in the oncreate method replace the background service class with foreground service instead of calling start service call start foreground service if you run the app the server should start however if we terminate the app it will stop this is because we have not put the service in the foreground state calling start foreground service only starts the service we still need to put in the foreground state go back to the foreground service class in the onstar command method call start foreground it takes two arguments an id for the notification and the notification itself the reason why we need to provide a notification for a foreground service is because we need to let the user know that this is a service from our app that's running even when the app is terminated the notification cannot be removed until the service is terminated pass in an id and a notification object if we run the app and terminate it the service will still be running and we'll see a notification in the notification tray although it is working as intended we still need to do one more thing to properly handle foreground services because they stay alive even after the app is terminated whenever the app gets relaunched another service will be created and we'll end up with multiple of the same servers running at the same time to stop this from happening every time we want to start the service we need to check if it is already running go back to the main activity file create a boolean method called foreground service running and have it return false by default inside the method get a reference to the activity manager using guest system service then use the for loop to loop through all the active services that are running during each iteration check if there is a service that matches our foreground service if there is return true in the oncreate method check if the service is not running first if it is not running we want to start the service if we run the app and then terminate it and then relaunch the ad it will not start the service again sometimes you may want to restart a foreground service when the user reboots the system we can achieve this by using a broadcast receiver the purpose of the broadcast receiver is to send or receive messages from the android system when the user reboots the device android sent out a message telling everyone that the system was rebooted we need to create a broadcast receiver to receive that message so we can restart the foreground service to create a broadcast receiver create a new class and have extended broadcast receiver class we want to receive a reboot message when the system was restarted override the on receive method now that we have the broadcast receiver we need to add it in the manifest file to let the app know about it go to the manifest file add a receiver element and use the name attribute to add the receiver inside the receiver add an intent filter to let the app know what the receiver will be listening for add the boot completed action so we listen for when the app is rebooted when that's done enable the receive bootcomplete permission now go back to the broadcast receiver class in the on receive method check if the action we receive from the intent is equal to action boot completed if it is we want to start the foreground service if we run the app and then restart the device the servers are start up automatically on his own that's all for this video if you find this video helpful please give it a like share and subscribe to support the channel see you in the next video you
Info
Channel: Codeible
Views: 2,549
Rating: undefined out of 5
Keywords: king, wai, mark, codeible, codeible.com, web, angular, java, android, typescript, html, css, @kingwaimark, Angular Material, Follow on Twitter, python, website, coding, money, education, finance, build, firebasets, npm, nodejs, Google, cloud firestore, android services, background service, foreground service, bound service, broadcastreceiver, android.permission.FOREGROUND_SERVICE, android.permission.RECEIVE_BOOT_COMPLETED, android.intent.action.BOOT_COMPLETED, Intent.ACTION_BOOT_COMPLETED
Id: bA7v1Ubjlzw
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Wed Sep 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.