14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9: Peer-to-Peer Communication<br />

The getUserData method returns a byte array of the data included in the message, while<br />

getUserDataHeader returns an array of metadata elements used to describe the data contained in<br />

the message.<br />

Emergency Responder SMS Example<br />

In this example, you’ll create an SMS application that turns an Android phone into an emergency<br />

response beacon.<br />

Once fi nished, the next time you’re in unfortunate proximity to an alien invasion or fi nd yourself in a<br />

robot-uprising scenario, you can set your phone to automatically respond to your friends’ and family<br />

members’ pleas for a status update with a friendly message (or a desperate cry for help).<br />

To make things easier for your would-be saviors, you’ll use location-based services to tell your rescuers<br />

exactly where to fi nd you. The robustness of SMS network infrastructure makes SMS an excellent<br />

option for applications like this where reliability and accessibility are critical.<br />

1. Start by creating a new EmergencyResponder project that features an EmergencyResponder<br />

Activity.<br />

package com.paad.emergencyresponder;<br />

import java.io.IOException;<br />

import java.util.ArrayList;<br />

import java.util.Locale;<br />

import java.util.concurrent.locks.ReentrantLock;<br />

import java.util.List;<br />

import android.app.Activity;<br />

import android.app.PendingIntent;<br />

import android.content.Context;<br />

import android.content.Intent;<br />

import android.content.IntentFilter;<br />

import android.content.BroadcastReceiver;<br />

import android.content.SharedPreferences;<br />

import android.location.Address;<br />

import android.location.Geocoder;<br />

import android.location.Location;<br />

import android.location.LocationManager;<br />

import android.os.Bundle;<br />

import android.telephony.gsm.SmsManager;<br />

import android.telephony.gsm.SmsMessage;<br />

import android.view.View;<br />

import android.view.View.OnClickListener;<br />

import android.widget.ArrayAdapter;<br />

import android.widget.Button;<br />

import android.widget.CheckBox;<br />

import android.widget.ListView;<br />

public class EmergencyResponder extends Activity {<br />

@Override<br />

297

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!