Adding Notifications and Toasts to the Earthquake Monitor

In the following example, the EarthquakeService is enhanced to trigger a Notification for each new earthquake. As well as displaying a status bar icon, the expanded Notification view will display the magnitude and location of the latest quake, and selecting it will open the Earthquake Activity.

1. Within the EarthquakeService, start by creating a new Notification instance variable to store the Notification object used to control the status bar icon and extended status window item details.

private Notification newEarthquakeNotification; public static final int NOTIFICATION_ID = 1;

2. Extend the onCreate method to create this Notification object.

©Override public void onCreate() {

updateTimer = new Timer("earthquakeUpdates");

int icon = R.drawable.icon;

String tickerText = "New Earthquake Detected"; long when = System.currentTimeMillis();

newEarthquakeNotification= new Notification(icon, tickerText, when);

3. Now return to the EarthquakeLookupTask implementation. Expand the onProgressUpdate stub to trigger the Notification after each new earthquake is added to the Content Provider. Before initiating the Notification, update the extended details using setLatestEventlnfo. Also create and display a new Toast to announce each new quake.

©Override protected void onProgressUpdate(Quake... values) { String svcName = Context.NOTIFICATION_SERVICE; NotificationManager notificationManager =

Prepared for ASHLEEKABAT, email:[email protected]:56760408This PDFisfor the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

Context context = getApplicationContext();

String expandedText = values[0].getDate().toString();

String expandedTitle = "M:" + values[0].getMagnitude() + " " +

values[0].getDetails(); Intent startActivitylntent = new Intent(EarthquakeService.this,

Earthquake.class);

Pendinglntent launchlntent =

PendingIntent.getActivity(context, 0, startActivitylntent, 0);

newEarthquakeNotification.setLatestEventInfo(context, expandedTitle, expandedText, launchlntent);

newEarthquakeNotification.when =

java.lang.System.currentTimeMillis();

notificationManager.notify(NOTIFICATION_ID, newEarthquakeNotification);

Toast.makeText(context, expandedTitle, Toast.LENGTH_SHORT).show();

4. The final step is to clear and disable Notifications within the two Activity classes. This is done to dismiss the status icon when the application is active.

4.1. Starting with the Earthquake Activity, modify the onCreate method to get a reference to the Notification Manager.

NotificationManager notificationManager;

©Override public void onCreate(Bundle savedlnstanceState) { [ ... existing onCreate ... ]

String svcName = Context.NOTIFICATION_SERVICE; notificationManager =

(NotificationManager)getSystemService(svcName);

4.2. Modify the onReceive method of the EarthquakeReceiver. As this is registered only (so it will execute only) when the Activity is active, you can safely cancel all earthquake Notifications here as soon as they're triggered.

©Override public void onReceive(Context context, Intent intent) { loadQuakesFromProvider();

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);

4.3. Next, extend the onResume method to cancel the Notification when the Activity becomes active.

©Override public void onResume() {

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);

IntentFilter filter;

filter = new IntentFilter(EarthauakeService.NEW_EARTHQUAKE_FOUND);

Prepared for ASHLEE KABAT, email: [email protected] Order number: 56760408 This PDF is for the purchaser's personal use in accordance with the Wrox Terms of Service and under US copyright as stated on this book's copyright page. If you did not purchase this copy, please visit www.wrox.com to purchase your own copy.

receiver = new EarthquakeReceiver(); registerReceiver(receiver, filter); super.onResume();

4.4. Repeat the same process with the EarthquakeMap Activity. NotificationManager notificationManager;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.earthquake_map);

ContentResolver cr = getContentResolver();

earthquakeCursor = cr.query(EarthquakeProvider.CONTENT_URI, null, null, null, null);

MapView earthquakeMap = (MapView)findViewById(R.id.map_view); earthquakeMap.getOverlays().add(new EarthquakeOverlay(earthquakeCursor));

String svcName = Context.NOTIFICATION_SERVICE;

notificationManager = (NotificationManager)getSystemService(svcName);

@Override public void onResume() {

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);

earthquakeCursor.requery();

IntentFilter filter;

filter = new IntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND); receiver = new EarthquakeReceiver(); registerReceiver(receiver, filter);

super.onResume();

public class EarthquakeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {

notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);

earthquakeCursor.requery();

MapView earthquakeMap = (MapView)findViewById(R.id.map_view); earthquakeMap.invalidate();

All code snippets in this example are part of the Chapter 9 Earthquake 3 project, available for download at Wrox.com.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment