Intro
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.
Click on the Screen Shots to see larger version of them.
Objectives
At the end of this lab you will be expected to know:
Activities
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.
IMPORTANT:
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()
When the send button is pressed, the sendSMS() method is called. This method should send an SMS text message to the phone number contained in the EditText with the Id of R.id.messageNumber. The text of the message should be retrieved from the EditText with the Id of R.id.messageText.
2.1.2 Testing SMS with the Emulator
If you don't have a SIM card for your device, or if you don't have a device you can test sending a text message on the Emulator. You will need to start two separate emulator instances. From there, you can specify the port number of the recipient Emulator as the phone number. The ADB will then deliver the text message to the other Emulator:
You should be able to run your application, and verify that it can send a text message.
2.2 Listening for SMS Status Changes
For this subsection, you will implement the functionality necessary to monitor and respond to changes to the status of your sent text message.
2.2.1 Fill in BroadcastReceivers
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.
2.2.2 Fill in initStatusReceivers()
The initStatusReceivers() method is called from onCreate(...). You need to register your new receivers to listen for status change broadcasts in this method.
2.2.3 Setup PendingIntents
The following is to be performed in the sendSMS() method. You will need to create two PendingIntent objects and pass them into your SMSManager.sendTextMessage(...) method call. PendingIntents are a way of creating an Intent to execute at a later time. They can either start a service, start an activity, or send a broadcast.
2.2.4 Cancel PendingIntents
When the user hits the reset button, the UI will be reset and the SMSActivity should no longer receive udpates from the message it has previously sent. When the reset button is pressed the cancelPendingIntents() method is called. Fill in this method so that it cancels the SMSActivity's PendingIntents.
You should now be able to run your application, send a text message and verify that the TextView displaying the status gets updated properly.
Note: You may not get notified when the message is delivered if you're sending texts through the emulator, don't worry about this.
3. Accelerometer
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()
Retrieve an instance of the SensorManager and use it to register this instance of AccelActivity as a SensorEventListener for the Accelerometer Sensor.
3.1.2 Fill In onSensorChanged(...)
When a sensor changes, and thus has a new measurement, the SensorManager will call SensorChangeListener.onSensorChanged(...) on all listeners that have registered with Sensor. It will pass in a SensorEvent object which contains data about the change. You need to fill in the onSensorChanged(...) method so that the new data is displayed on the screen:
Run your application to ensure that the Accelerometer values are properly updated on the screen. Remember, you have to do this on a physical device.
4. Deliverables
To complete this lab, you will be required to:
Primary Author: James Reed
Adviser: Dr. David Janzen