Sample Application - Part 2
IntroductionIn this blog post, we will be talking about two additional features that can be fundamental in programming with Android. These two features will be Services and Broadcast Receivers. This blog post is part of a multipart series in which we cover various Android lessons. I would recommend taking a look at the first blog post as this will be extending the content from there.
Getting StartedIn the last example we covered the basic Activity. The Activity is Android's version of a user facing application. A Service on the other hand, perform functions in the background and in many cases will not directly interact with the user at all. The Broadcast Receiver, which is part of the Activity, will replace the Handler from Part 1, and will be receiving the information from our Service. Once received, the Activity will display the information to the user.
Removing the OldIn order to begin this change, we need to understand what sections we are replacing. The Counter class will no longer be an Observable and the CounterUISample class will no longer be a Observer, instead it will be sending Broadcasts to any Broadcast Receivers that are listening for this specific event. Which also means that we no longer need a Handler to handle the message sent from the update method as there will no longer be an update method. Below are the changes to remove the Observer/Observable and Handler from our old code.
Broadcast ReceiverAs you can see we removed all the places where we called the Handler and Observable/Observer. Now we need to change Counter so that it sends out Broadcasts that we can later receive with a Broadcast Receiver. In order to send Broadcasts, we need a context in which to send it from. This can be added by passing the context through the constructor to the Counter.
Constructor of Counter.java
Using this context we can send messages within the run method of the CounterWorkerThread. In order to send messages we actually send what are called Intents. These Intents describe what its action is, or what you need to call in order to receive this Intent, and can contain information in a Bundle. A Bundle is just a group of information that can be stored together.
Within the run method there is a few things that need to be added.
Run method from Counter.java
The Counter is now sending out Broadcasts thus, we need a Broadcast Receiver to receive them. We need to edit the CounterUISample so that it registers with the given Broadcast Receiver and implements the onReceive method in order to make changes when it receives the message.
Within onStart in CounterUISample.java
As you can see we make a new Broadcast Recevier which has its own onReceive method. In the onReceive method, we extract the information from the Intent. We then change the TextView to display the information from the Counter class. We also created an Intent Filter which will specify which Intent we are listening for. In this case, we are listening for the Action we provided above. Using this Broadcast Receiver and Intent Filter, we are able to register the receiver.
The last change in order to make this Broadcast to be received by this Activity would be to modify the manifest. We add this in the application section of the manifest.
Within activity section of Android Manifest
This is just an additional Intent Filter added to this application that again has the same action. In the onStart and onStop methods we just need to start and stop the Counter.
This is a good opportunity to show off a Service. The Counter is a background operation that should continue to count away regardless of what the User Interface is doing. In this way, the counter should be a Service. A Service in Android is similar to an Activity as it follows states in its operational lifetime but has its own distinct lifecycle. We will be dealing with two specific states, onCreate and onDestroy.
We start by creating a new class called CounterService and having it extend Service. Service has one method that must be overwritten which is the onBind method.
We then instantiate a Counter and a constructor for CounterService. In the constructor we proceed to create a Counter.
In order for this Service to actual do something we need to overwrite the onCreate method. In this method we start the Counter. Additionally we need to stop the counter when the program is no longer running. We do this by overwriting the onDestroy method and having it stop the Counter.
We need to edit the manifest so that it knows that this Service exists.
Within the application section of Android Manifest
After making all of these changes we are able to run the program again. It should perform the same actions before but, instead of using a Observable/Observer relation and a Handler we are able to use Broadcast Receiver and Services. Although the additional Service adds no extra usability now, if we wanted to extended this program so that other applications on the Android could use this Service it would be much easier.
ConclusionIn this blog post, we were able to go over a couple of key things.
All of these are valuable things to know when writing applications on an Android device.
By MatthewT Williams
Subscribe to our Cius Developer Blog RSS feed to get the latest info and sample code.