Go to http://sites.google.com/site/androidappcoursev3/ for the most up-to-date versions of these labs.
For this lab you will be filling in a new Application that simply demonstrates a few key features of the Android framework. In particular, the application demonstrates how to send SMS text messages and how to monitor motion of the device through the Accelerometer. The purpose of the application is simply to demonstrate functionality. When the application starts up, the user is presented with an Activity that allows them to choose which feature they would like to demo by pressing one of two buttons, either SMS or Accelerometer monitoring. When the user hits one of the buttons, it launches the Activity for the selected demo.
At the end of this lab you will be expected to know:
For this lab you will be working with a brand new application, completely independent of the previous labs. Once you have a general understanding of the components of the application, you will begin implementing it.
You will be given a Skeleton Project to work with. This project contains all of the java and resource files you will need to complete the lab. Some method stubs, member variables, and resource values and ids have been added as well. It is important that you not change the names of these methods, variables, and resource values and ids. These are given to you because there are unit tests included in this project as well that depend on these items being declared exactly as they are. These units test will be used to evaluate the correctness of your lab. You have complete access to these test cases during development, which gives you the ability to run these tests yourself. In fact, you are encouraged to run these tests to ensure that your application is functioning properly.
1. Setting Up...
1.1 Creating the Project
To begin, you will need to download and extract the skeleton project for the Lab 7 application.
Click Here to download the skeleton project.
Extract the project, making sure to preserve the directory structure.
Take note of the path to the root folder of the skeleton project.
Next you will need to setup an Android project for this app. Use the settings listed below for the remaining fields:
1.2 Familiarize Yourself with the Project
The project contains three java files and three XML layout files. Lab7.java defines a simple Activity that allows you to choose which demo you want to start. The Lab7 class makes use of a very simple XML layout file called main.xml. Both Lab7.java and main.xml have been completed for you.
SMSActivity.java defines a simple SMS text messaging Activity. It allows the user to enter a phone number, a textual message, and send the an SMS text message. It also listens to the status of the sent SMS text message and updates the UI to display changes to the status. The SMSActivity class makes use of an XML layout file called sms.xml which has been completed for you. SMSActivity.java has been partially completed. The UI components have been hooked up and initialized. It will be your job to implement functionality for sending the text message and responding to status changes.
When the device moves, the Accelerometer sensor in the device measures that movement. AccelActivity.java defines a simple Accelerometer monitoring Activity. It only displays measurements from the device's Accelerometer to the user. It makes use of a simple XML layout file called accel.xml which has been completed for you. AccelActivity.java has been partially completed for you. It is your job to fill in the code necessary to listen for measurements from the Accelerometer and update the display.
2. SMS Activity
In this section of the lab, you will be learning how to send an SMS text message and listen for changes in its delivery status. All the work done in this section will be performed in the SMSActivity.java file. Access to SMS is done through the android.telephony.SmsManager class. It provides an interface for sending both text and data messages, as well as listening for status changes of sent messages and delivery of incoming messages. For this lab, we will only be concerned with sending text messages and monitoring the status of those messages. These exercises should give you enough information to start working with SMS on your own. Data messages are sent in almost the same way as text messages, and monitoring of incoming messages is done through BroadcastReceivers. Both should be fairly straightforward to figure out on your own.
2.1 Sending an SMS Text Message
For this subsection, you must simply send an SMS text message.
2.1.1 Fill in sendSMS()
Status changes of a sent SMS message are reported through Intent broadcasts. Therefore, in order to respond to these changes you will need a BroadcastReceiver, two in fact. There are two separate status changes that get broadcasted. The first status change gets broadcast once the message has been sent. The second is broadcast once the message has been delivered.
In this section of the lab, you will be learning how to monitor changes to the Accelerometer sensor. All work in this section will be performed in the AccelActivity.java file. As is usually the case, you will not be interacting with the Accelerometer sensor directly, but will instead be using the android.hardware.SensorManager class. This class provides you with a set of API's for retrieving Sensor objects, querying Sensors for measurements, and registering listeners with Sensors. The Sensor objects themselves do not provide you with measurements. Sensor objects only provide hardware information like vendor, resolution, and range.
There are a number of different types of Sensors from which data can be obtained. Examples include Magnetic, Light, and Temperature. The ones that you can access depend entirely on which Sensors the device actually has. For this lab you will only be interacting with the Accelerometer Sensor. Interaction with other sensors is performed in the same manner and this will give you a good starting point to learn the rest on your own.
3.1 Monitoring Device Motion
In the subsections that follow, you register the AccelActivity to be notified of any changes to the Accelerometer. When notified of changes you will simply display the raw data to the screen. There Android SDK does not currently provide a method for simulating the Accelerometer on the Emulator, so the following section must be tested on a physical device to ensure that it is working properly.
3.1.1 Fill in registerWithSensor()
To complete this lab, you will be required to:
Primary Author: James Reed
Adviser: Dr. David Janzen