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 8: Working in the Background<br />

270<br />

}<br />

@Override<br />

public void onResume() {<br />

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);<br />

}<br />

earthquakeCursor.requery();<br />

IntentFilter filter;<br />

filter = new IntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);<br />

receiver = new EarthquakeReceiver();<br />

registerReceiver(receiver, filter);<br />

super.onResume();<br />

public class EarthquakeReceiver extends BroadcastReceiver {<br />

@Override<br />

public void onReceive(Context context, Intent intent) {<br />

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);<br />

}<br />

}<br />

earthquakeCursor.requery();<br />

MapView earthquakeMap = (MapView)findViewById(R.id.map_view);<br />

earthquakeMap.invalidate();<br />

Advanced Notifi cation Techniques<br />

In the following sections, you’ll learn to enhance Notifi cations to provide additional alerting through<br />

hardware, in particular, by making the device ring, fl ash, and vibrate.<br />

As each enhancement is described, you will be provided with a code snippet that can be added to the<br />

Earthquake example to provide user feedback on the severity of each earthquake as it’s detected.<br />

To use the Notifi cation techniques described here without also displaying the status bar icon, simply<br />

cancel the Notifi cation directly after triggering it. This stops the icon from displaying but doesn’t interrupt<br />

the other effects.<br />

Making Sounds<br />

Using an audio alert to notify the user of a device event (like incoming calls) is a technique that predates<br />

the mobile, and has stood the test of time. Most native phone events from incoming calls to new<br />

messages and low battery are announced by an audible ringtone.<br />

Android lets you play any audio fi le on the phone as a Notifi cation by assigning a location URI to the<br />

sound property, as shown in the snippet below:<br />

notification.sound = ringURI;<br />

To use your own custom audio, push the fi le onto your device, or include it as a raw resource, as<br />

described in Chapter 6.

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

Saved successfully!

Ooh no, something went wrong!