Android Basic Training Course: Basic Services Patterns

In the previous post, I presented the Service theory, through that you can see the components that build up the Service and it's clients, now we are going to see some examples using the Service and the way how the these scenario executed.

IntentServices for one time tasks: Download File Service

You can also extend the IntentService class for your service implementation.
The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates itself automatically. An example for its usage would be downloading certain resources from the internet.
The IntentService class offers onHandleIntent() method which will be asynchronously called by the Android system.DEMO VIDEO:

Firstly in this "download file by Service" project, creating an activity layout file simple like this:

Now, go to programmatically code, as note above, we must create a subclass of IntentService, mean that this Service run one time and auto destroy after finish downloading file process.
By using this Service type, the most important work is overriding the onHandleIntent(), in this example, we create an InputStream to get data from an URL (a PNG image):

Note: As you see in code, we publish download result (Error or Success) to UI thread by BroadcastReceiver. In Service class, this action will invoke by call sendBroadcast(Intent).
In this activity, we will talk more clearly about the BroadcastReceiver. Declaring an instance to receive the Service result and update data to views, overriding onReceive() is required, receiving Bundle put from Service here:

Unbounded Service: Audio Player

Most audio player app in Android does not require the user to be in applications while listening to music. Conversely, the user can do other works with their devices, while music application will run background. This is very similar to the download process mentioned above. However, in this case, the user must stop playing music process.
Creating a simple activity layout includes 2 Button (to Start and Stop playing Music):

We will create a Service's subclass and overriding some necessary methods. In onBind() we will return an IBinder instance (may be null), because onStartCommand() will be overridden, starting MediaPlayer here:

This app not require any special permission. From now on, when you click Start button, app will play your audio file, and as you see in the activity code, I used startService() (bindService()), so this Service will always run in the background even your activity destroyed. It's only stop when you hit stop button.
Note: Audio resource files must be put in res/raw folder. All Android Supported Media format you can find out here!

Conclusions

By this post, I have present 2 sample project about Service in Android corresponding to 2 Service types: IntentService and normal (Unbounded) Service. I hope that you can help you to understand the philosophy of Service action. Further, you can go to these official docs to reading more: