pages

Sunday, August 15, 2021

#Android Development : Services 1/4 : The Basics

        #Finally, Jetpack Compose 1.0 was officially incorporated three weeks ago. Upon hearing it , I was so excited about how Jetpack Compose will affect android developers. It has already been with us for a long time actually so we have already known what kind of function it has. But today I'm going to be talking about a different topic I've wondered a lot. 'Services' in programming android apps... 


#Android Services


   Services are one of application components and they can be used as a long-running process executer in the background. A service can keep running until a certain time , even after the user closes the app.


#These are the three different types of services:

1-Foreground

2-Background

3-Bound


//FOREGROUND SERVICE//

   A foreground service performs some operation that is noticeable to the user. So you must declarate it in the manifest.xml also you must create a notification about the the foreground service which is running at the moment. so that users are actively aware that the service is running.Actually it's a must , you have no choices without exception.


Note : Using Work Manager is more reasonable than using directly a foreground service , indeed. But I can't find sufficient source about making services in programming android.


//BACKGROUND SERVICE:

    A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service. "downloading process..etc"


//BOUND SERVICE:

     A service is bound when an application component binds to it by calling bindService().  A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed. As far as I see it , you have to bind your activities or other components with the bound service to get the results you would like to see.


#A Service or A Thread ,Which one is useful for you?

    First things first , you have to know a service doesn't have a thread or  a separate process area, I mean a service is not the same as a thread at functionallity. We use services to create long-running process steadily for a certain time without letting users notice what's going on in the app. But if a service runs in the main thread with a long-running process , it blocks the users UI especially blocking operations such as dowloading content from a website , maybe it's a video. Here it is!!! Downloading a video from internet blocks users' UI for a long time unless the process runs in a separate thread , to give example , default thread, IO ..etc 

   You should create a service only if that is what you need. If you must perform work outside of your main thread, but only while the user is interacting with your application, Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.


    To create a service, you must create a subclass of Service or use one of its existing subclasses. In your implementation, you must override some callback methods that handle key aspects of the service lifecycle and provide a mechanism that allows the components to bind to the service, if appropriate.


onStartCommand() -> There are several options you have here, this function let you choose one of them and returnes an integer which emphasize how the service behaves when something goes wrong. And I'm gonna mention them later on.

onBind() -> if you don't need it , just get it returned null, but you have to implement it in the every scene.

onCreate() -> You know what this means , indeed :)

onDestroy() -> these are the same as the ones of activities.


  #You must declare all services in your application's manifest file, just as you do for activities and other components.


Note: 

    Users can see what services are running on their device. If they see a service that they don't recognize or trust, they can stop the service. In order to avoid having your service stopped accidentally by users, you need to add the android:description attribute to the <service> element in your app manifest. In the description, provide a short sentence explaining what the service does and what benefits it provides. And if you do it , you will allow users to know what those do in their phones.


    When a service is running, it can notify the user of events using Toast Notifications or Status Bar Notifications. And these may be useful to let users know what are the app doing now. I will learn it and mention it at my next articles.







THE INTEGER CONSTANT VALUES ADJUSTING HOW A SERVICE RUNS:

    # I prefer to use the title actually it might be confusing for you but I think it will clear everything I will mention. 


START_NOT_STICKY
    If the system kills the service after onStartCommand() returns, do not recreate the service unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.
START_STICKY
    If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent unless there are pending intents to start the service. In that case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands but are running indefinitely and waiting for a job.
START_REDELIVER_INTENT
    If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file.

           For more details about these return values, see the linked reference documentation for each constant.



No comments:

Post a Comment