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

4.1. Starting with the Earthquake Activity, modify the onCreate method to get a reference<br />

to the Notifi cation Manager.<br />

NotificationManager notificationManager;<br />

@Override<br />

public void onCreate(Bundle icicle) {<br />

[ ... existing onCreate ... ]<br />

}<br />

String svcName = Context.NOTIFICATION_SERVICE;<br />

notificationManager = (NotificationManager)getSystemService(svcName);<br />

4.2. Modify the onReceive method of the EarthquakeReceiver. As this is only registered<br />

(so it will only execute) when the Activity is active, you can safely cancel<br />

all Notifi cation earthquake Notifi cations here as soon as they’re triggered.<br />

@Override<br />

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

loadQuakesFromProvider();<br />

}<br />

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

4.3. Next, extend the onResume method to cancel the Notifi cation when the Activity<br />

becomes active.<br />

@Override<br />

public void onResume() {<br />

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

}<br />

IntentFilter filter;<br />

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

receiver = new EarthquakeReceiver();<br />

registerReceiver(receiver, filter);<br />

super.onResume();<br />

4.4. Repeat the same process with the EarthquakeMap Activity.<br />

NotificationManager notificationManager;<br />

@Override<br />

public void onCreate(Bundle icicle) {<br />

super.onCreate(icicle);<br />

setContentView(R.layout.earthquake_map);<br />

ContentResolver cr = getContentResolver();<br />

earthquakeCursor = cr.query(EarthquakeProvider.CONTENT_URI,<br />

null, null, null, null);<br />

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

earthquakeMap.getOverlays().add(new EarthquakeOverlay(earthquakeCursor));<br />

String svcName = Context.NOTIFICATION_SERVICE;<br />

notificationManager = (NotificationManager)getSystemService(svcName);<br />

269

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

Saved successfully!

Ooh no, something went wrong!