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

268<br />

2. Extend the onCreate method to create this Notifi cation object.<br />

@Override<br />

public void onCreate() {<br />

updateTimer = new Timer(“earthquakeUpdates”);<br />

}<br />

int icon = R.drawable.icon;<br />

String tickerText = “New Earthquake Detected”;<br />

long when = System.currentTimeMillis();<br />

newEarthquakeNotification= new Notification(icon, tickerText, when);<br />

3. Now extend the announceNewQuake method to trigger the Notifi cation after each new earthquake<br />

is added to the Content Provider. Before initiating the Notifi cation, update the extended<br />

details using setLatestEventInfo.<br />

private void announceNewQuake(Quake quake) {<br />

String svcName = Context.NOTIFICATION_SERVICE;<br />

NotificationManager notificationManager;<br />

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

}<br />

Context context = get<strong>Application</strong>Context();<br />

String expandedText = quake.getDate().toString();<br />

String expandedTitle = “M:” + quake.getMagnitude() + “ “ +<br />

quake.getDetails();<br />

Intent startActivityIntent = new Intent(this, Earthquake.class);<br />

PendingIntent launchIntent = PendingIntent.getActivity(context,<br />

0,<br />

startActivityIntent,<br />

0);<br />

newEarthquakeNotification.setLatestEventInfo(context,<br />

expandedTitle,<br />

expandedText,<br />

launchIntent);<br />

newEarthquakeNotification.when = java.lang.System.currentTimeMillis();<br />

notificationManager.notify(NOTIFICATION_ID, newEarthquakeNotification);<br />

Intent intent = new Intent(NEW_EARTHQUAKE_FOUND);<br />

intent.putExtra(“date”, quake.getDate().getTime());<br />

intent.putExtra(“details”, quake.getDetails());<br />

intent.putExtra(“longitude”, quake.getLocation().getLongitude());<br />

intent.putExtra(“latitude”, quake.getLocation().getLatitude());<br />

intent.putExtra(“magnitude”, quake.getMagnitude());<br />

sendBroadcast(intent);<br />

4. The fi nal step is to clear and disable Notifi cations within the two Activity classes. This is done to<br />

dismiss the status icon when the application is active.

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

Saved successfully!

Ooh no, something went wrong!