/All-About-Services

A repository dedicated to learning, exploring and delving into the services component/class of Android.

Primary LanguageKotlin

Service

Three types of services

  • ForeGround
  • Background
  • Intent Service - creates a new thread for execution of task on backgroud. we don't have to manage creating new thread. intent services works the same way like normal service if it is bound. Functionality wise it remains the same as service.
  • Bound

Two ways to start a STARTED Services
1) startService() method
2) startForegroundService() method


Two ways to start a BOUND Services
1) call startService()/startForegroundService() method and then bind client to it.
2) directly binding client to service using bindService() method.

LifeCycle methods of services

  1. onStartCommand
  2. onBind
  3. onUnbind
  4. onRebind
  5. onCreate
  6. onDestroy
  7. onTaskRemoved() <-- called when application is removed from recents tab. Call stopSelf() here.

We must explicitly set the service in the intent.

If we start a service by just calling bindService() then it will be destroyed when its clients are destroyed. But is we start by using startService() or startForegroundService() then it will be killed by stopSelf only.

By default service runs on thread on which it is invoked. To avoif this we have to manually create a new thread or provide some sort of dispatcher to make it run on bg thread. https://developerlife.com/2017/07/10/android-o-n-and-below-component-lifecycles-and-background-tasks/

Read about how services can be restarted using some contents.

  1. START_STICKY: The system restarts the service after termination but doesn't redeliver the last intent. Useful for services that can continue independently. This is useful for services that are performing tasks that don't rely on specific Intent data and can continue running independently.

  2. START_NOT_STICKY: The system doesn't automatically restart the service if it's terminated. Suitable for one-time or non-persistent tasks. If you have a service that performs a one-time data synchronization task, you might use START_NOT_STICKY because it doesn't make sense to restart the service after the task is completed

  3. START_REDELIVER_INTENT: The system restarts the service after termination and redelivers the last intent. Useful for critical tasks that require resuming work with the original intent data after termination. If you have a service that's downloading a large file, you would use START_REDELIVER_INTENT so that even if the service is terminated, it can resume the download from where it left off by redelivering the download Intent.

Service which provides information to activity or another service is called bound service. If the service is bound to the components of the same app, then it is local binding. With other app it becomes remote binding.this remote binding can be though of as IPC (Inter Process Communication)

Two types of bound services Local binding - implemented using IBinder interface. Remote binding - implemented using Messenger API - AIDL (Android Interface Definition language) (Not recommended to use)

Local Binding A bound service cannot be stopped. We need to unbind it first. If we try to stop an unbound service it wont stop. But as soon as its unbounded itll be stopped and destroyed. OnBind is only called once to bind the service. Later calls are ignored


Notification

Note - A channel is needed to show notifications. If we try to send notification before creating channel we won't be able to see the notification. because it won't even trigger.

Modifying notification from code and its visial effect

1. LongText(BigText) Style and big context title.


Screenshot 2023-03-01 015945



WhatsApp Image 2023-03-01 at 1 58 03 AM



WhatsApp Image 2023-03-01 at 1 58 03 AM (1)


2. For BigPicture Style and Media Style Notification - Refer this video https://www.youtube.com/watch?v=s0Q2QKZ4OP8&list=PLrnPJCHvNZuDR7-cBjRXssxYK0Y5EEKzr&index=5

Note - From API level 24 onwards notification from an app are grouped together if there are multiple of them. So it is handled automatically.

Note - Multiple channels can be grouped under one channel group. If we don't put a channel in a group then by default it goes to other(default) channel.

Note - For custom layout notification, refer to this video https://www.youtube.com/watch?v=axcdnRAcqLw&list=PLrnPJCHvNZuDR7-cBjRXssxYK0Y5EEKzr&index=12


More references for Music Player https://github.com/topics/android-music-player?l=kotlin

LLD for media player that I have made

image

Services related android interview question resources

  1. https://climbtheladder.com/android-service-interview-questions/
  2. https://stacktips.com/articles/android-service-interview-questions
  3. https://www.interviewbit.com/android-interview-questions/