Services in Android . BASICS why we use Services instead of Aysnctask loader.
Android service is a component that is used to perform operations on the background such as playing music, handle network transactions, interacting content providers etc. It doesn't has any UI (user interface).The service runs in the background indefinitely even if application is destroyed.
THREADS, ASYNCTASK & ASYNCTASKLOADER is fine if you want to carry not so long task in the background i.e for few seconds.
Common Types of Services :
1] Started service
2] Intent Service
3] Bound service
4] Foreground service
But , if you want carry a task for a long period and even after an application is closed, then we use SERVICES. long task - eg Music player.
After Android 8.0 there were put limitations on Background running processes , since they drain alott of battery , hence SERVICES DONT RESTART after Android 8 or API 25 ,it it recommmended to use JOBSCHEDULER.
THIS LIMITS DOESNT APPLY FOR FOREGROUND SERVICES & BOUND SERVICES AT SOME EXTENT.
useful video on limitations after API 25 : https://youtu.be/LarihH4b6Z4
By Default ONLY INTENTSERVICE runs on WORKER THREAD, all other services run on UI THREAD.
REMEMBER TO ADD ALL SERVICES TO MANIFEST FILE
--------------------------------------------------------------------------------------------------------------------
ALL SERVICES ARE FROM JAVA CLASS.
Services have NO layout.
API level 26 Android 8.0 has certain limitations on background tasks
onBind() method is to be overrided on both Started services and Bound services
-----------------------------------------------------------------------------------------------------------------------
STARTED SERVICE :
Runs on the main thread , hence we need to create seperate thread or handler to hande UI form started service.
Used only for short processes since it runs on main thread and can freeze ui.
We can pass data using intent, but there is no direct way to get data return.
We use Result reciever or broadcast reciever to get data back from started service.
In case of started services the OnBind() method returns a null.
You can also use Asynctask inside Started service :
video -https://youtu.be/Y2ZYiYTomLA
The return type of onStartCommand() decided how the Service should react , i.e should it restart or not and stuff.
The returns type of onStartcommand() of a service decides how it'll behave when its terminated or removed when resources are low.
useful video on this topic : https://youtu.be/t_QrhnVIcHw
Video on onstartcommand return types : https://youtu.be/_vTPvKMGkUk
create and execute started service :
started service lifecycle & Stopping a started service :
update ui or send data from service to Activity :
------------------------------------------------------------------------------------------------------------------------
INTENT SERVICE :
Most comman service to be used.
This is a BETTER option then started service.
INTENT SERVICE EXTENDS THE STARTED SERVICE.
RUNS ON A WORKER THREAD unlike started service which runs on main thread and be default only ONE worker thread is allocatted.
No need to stop the service using stopself() or stopservice() , it Stops Automatically after completion of task.
Intent Service handle most of the task for handler and loopers internally.
After android 8 or Oreo , there are different approaches to use this , as there are limitations for background programming after android oreo.
useful videos :
intro : https://youtu.be/XZ-LIgm9ILI
create and execute intentservice :
------------------------------------------------------------------------------------------------------------------------
BOUND SERVICE :
RUNS ON MAIN THREAD.
We cannot use Bound Service with Broadcast Receivers .
It uses the onBind() method and returns a Ibinder.
If the calling component gets Destroyed the Bound service gets destroyed as well.
started by bindservice() method and stopped from unbindservice() method.
intro video :
https://youtu.be/hXC9qM3nUeI
create & execute bound service :
------------------------------------------------------------------------------------------------------------------------
FOREGROUND SERVICE :
Forground services MUST display a Notification.
This service cannot be Terminated until the process gets completed. and runs even when the app is running.
The Notification stays until the Process is complete.
blog :
-------------------------------------------------------------------------------------------------------------------
JOB SCHEDULER :
----------------------------------------------------------------------------------------------







Comments
Post a Comment