Learn by doing Android — Services and IntentServices(1/3)Shivam DhuriaBlockedUnblockFollowFollowingMay 28This is a 3 part series.
Part 2 — Bound ServicesPart 3 — Intent ServicesWhat is a Service?It is an application component that runs without a direct interaction with the user.
It has no user interface.
It is mostly used for short tasks.
However for longer running tasks we may need to create another thread within the service as service runs on main thread of the calling Component’s process by default which may lead to ANR.
It runs in the background even if the application has been destroyed.
They run with a higher priority than invisible/inactive activities.
A good example of it is to think of a music streaming service (Apple Music,Spotify) that still runs(plays music) in the background even if the app is in back stack.
Services can be classified into three typesForeground ServiceIt has the highest priority among all the types of services and android requires you to display a notification whenever you run this particular service.
It runs with the same priority as that of an activity.
This notification cannot be dismissed unless the service is stopped.
This basically lets the user be informed about what service is actively running that may drain the battery.
Learn By Doing.
We’ll create a simple application that starts a foreground service which plays a track in loop when startServiceButton is clicked.
The track keeps playing until the stopServiceButton is pressed.
You can see in the .
gif above that the foreground service notification cannot be dismissed.
javaWe need to create a notification channel for > = Android Oreo.
Make sure you add this class and ServiceForeground in the manifest file too.
javaWe launch the foreground via intent.
javaThese return values are only relevant when the phone runs out of memory and kills the service before it finishes executing.
(Read More)START_STICKY: If the service gets restarted because its process is killed, onStartCommand() will be called on the next instance of the service with a null Intent instead of not being called at all.
Services that use this mode should always check for this case and deal with it appropriately.
START_NOT_STICKY: after returning from onStartCreated(), if the process is killed with no remaining start commands to deliver, then the service will be stopped instead of restarted.
This makes a lot more sense for services that are intended to only run while executing commands sent to them.
START_REDELIVER_INTENT: This is like START_NOT_STICKY, except if the service’s process is killed before it calls stopSelf() for a given intent, that intent will be re-delivered to it until it completes (unless after some number of more tries it still can’t complete, at which point the system gives up).
This is useful for services that are receiving commands of work to do, and want to make sure they do eventually complete the work for each command sent.
xmlBackground ServicesStarting from API 26, the system will terminate your service if you start it without showing notification.
It will close the background service as soon as your application isn’t in the foreground.
Hence they aren’t a recommended approach.
This was the part 1 of the series, part two will be published soon.
Code for the above project is available on my Github.
Android Thread Constructs(Part 4): ComparisonsIn this series of posts we have seen the following thread constructs: 1.
Basic threads and communication between them…techtej.
comUnderstanding Android Started and Bound ServicesIn Android, a Service is an application component that can perform long-running operations in the background on the UI…codetheory.
inAndroid Services – TutorialA foreground service is a service that should have the same priority as an active activity and therefore should not be…www.