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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

The following skeleton code shows how to register a simple Broadcast Receiver implementation that can<br />

handle the Intent transmitted in the previous example:<br />

BroadcastReceiver otaGTalkIntentReceiver = new BroadcastReceiver() {<br />

@Override<br />

public void onReceive(Context _context, Intent _intent) {<br />

if (_intent.getAction().equals(ACTION_OTA_ELIMINATE)) {<br />

String sender = _intent.getStringExtra(“sender”);<br />

String target = _intent.getStringExtra(“target”);<br />

}<br />

};<br />

}<br />

String lat = _intent.getStringExtra(“lat”);<br />

String lng = _intent.getStringExtra(“long”);<br />

Location location = new Location(LocationManager.GPS_PROVIDER);<br />

location.setLatitude(Double.parseDouble(lat));<br />

location.setLongitude(Double.parseDouble(lng));<br />

// TODO: Do something with the data transmitted.<br />

IntentFilter filter = new IntentFilter(ACTION_OTA_ELIMINATE);<br />

registerReceiver(otaGTalkIntentReceiver, filter);<br />

Introducing SMS<br />

If you own a mobile phone that’s less than two decades old, chances are you’re familiar with SMS messaging.<br />

SMS (short messaging service) is now one of the most-used features on mobile phones, with<br />

many people favoring it over making phone calls.<br />

SMS technology is designed to send short text messages between mobile phones. It provides support for<br />

sending both text messages (designed to be read by people) and data messages (meant to be consumed<br />

by applications).<br />

As a mature mobile technology, there’s a lot of information out there that describes the technical details<br />

of how an SMS message is constructed and transmitted over the air. Rather than rehash that here, the<br />

following sections focus on the practicalities of sending and receiving text and data messages within<br />

Android.<br />

Using SMS in Your <strong>Application</strong><br />

Android offers full access to SMS functionality from within your applications with the SMSManager.<br />

Using the SMS Manager, you can replace the native SMS application or create new applications that<br />

send text messages, react to incoming texts, or use SMS as a data transport layer.<br />

SMS message delivery is not timely, so SMS is not really suitable for anything that requires real-time<br />

responsiveness. That said, the widespread adoption and resiliency of SMS networks make it a particularly<br />

good tool for delivering content to non-Android users and reducing the dependency on third-party servers.<br />

291

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

Saved successfully!

Ooh no, something went wrong!