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 />

304<br />

}<br />

lock.unlock();<br />

SmsManager sms = SmsManager.getDefault();<br />

// Send the message<br />

sms.sendTextMessage(_to, null, _response, null, null);<br />

StringBuilder sb = new StringBuilder();<br />

// Find the current location and send it as SMS messages if required.<br />

if (_includeLocation) {<br />

String ls = Context.LOCATION_SERVICE;<br />

LocationManager lm = (LocationManager)getSystemService(ls);<br />

Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);<br />

}<br />

sb.append(“I’m @:\n”);<br />

sb.append(l.toString() + “\n”);<br />

List addresses;<br />

Geocoder g = new Geocoder(get<strong>Application</strong>Context(), Locale.getDefault());<br />

try {<br />

addresses = g.getFromLocation(l.getLatitude(), l.getLongitude(), 1);<br />

if (addresses != null) {<br />

Address currentAddress = addresses.get(0);<br />

if (currentAddress.getMaxAddressLineIndex() > 0) {<br />

for (int i = 0; i < currentAddress.getMaxAddressLineIndex(); i++) {<br />

sb.append(currentAddress.getAddressLine(i));<br />

sb.append(“\n”);<br />

}<br />

}<br />

else {<br />

if (currentAddress.getPostalCode() != null)<br />

sb.append(currentAddress.getPostalCode());<br />

}<br />

}<br />

} catch (IOException e) {}<br />

ArrayList locationMsgs = sms.divideMessage(sb.toString());<br />

for (String locationMsg : locationMsgs)<br />

sms.sendTextMessage(_to, null, locationMsg, null, null);<br />

At the time of production, sendTextMessage required a non-null value for the fourth parameter<br />

(sentIntent). In this example, this parameter is added in Step 13, so running the application now<br />

will cause it to throw an exception.<br />

13. In emergencies, it’s important that messages get through. Improve the robustness of the application<br />

by including auto-retry functionality. Monitor the success of your SMS transmissions so<br />

that you can re-broadcast a message if it doesn’t successfully send.

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

Saved successfully!

Ooh no, something went wrong!