Login     Sign Up
Tugadar (@admin)
15 days ago

In this final modification to the Earthquake example, you’ll use Alarms to replace the Timer currently used to schedule Earthquake network refreshes.

Start by creating a new EarthquakeAlarmReceiver class that extends BroadcastReceiver.

package com.paad.earthquake;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

public class EarthquakeAlarmReceiver extends BroadcastReceiver {


Override the onReceive method to explicitly start the EarthquakeService.


public void onReceive(Context context, Intent intent) {

Intent startIntent = new Intent(context, EarthquakeService.class); context.startService(startIntent);


Create a new public static String to define the action that will be used to trigger the Broadcast Receiver.

public static final String ACTION_REFRESH_EARTHQUAKE_ALARM = “com.paad.earthquake.ACTION_REFRESH_EARTHQUAKE_ALARM”;

Add the new EarthquakeAlarmReceiver to the manifest, including an intent-filter tag that listens for the action defined in Step 3.

<receiver android:name=”.EarthquakeAlarmReceiver”> <intent-filter>






Within the EarthquakeService, update the onCreate method to get a reference to the AlarmManager, and create a new PendingIntent that will be fired when the Alarm goes off. You can also remove the timerTask initialization.

AlarmManager alarms;

PendingIntent alarmIntent;


public void onCreate() {

int icon = R.drawable.icon;

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

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

alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


ALARM_ACTION = EarthquakeAlarmReceiver.ACTION_REFRESH_EARTHQUAKE_ALARM; Intent intentToFire = new Intent(ALARM_ACTION);

alarmIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);


Modify the onStart method to set an Alarm rather than use a Timer to schedule the next refresh (if automated updates are enabled). Setting a new Intent with the same action will auto-matically cancel the previous Alarm.


public void onStart(Intent intent, int startId) {

SharedPreferences prefs = getSharedPreferences(Preferences.USER_PREFERENCE, Activity.MODE_PRIVATE);

int minMagIndex = prefs.getInt(Preferences.PREF_MIN_MAG, 0); if (minMagIndex < 0)

minMagIndex = 0;

int freqIndex = prefs.getInt(Preferences.PREF_UPDATE_FREQ, 0); if (freqIndex < 0)

freqIndex = 0;

boolean autoUpdate = prefs.getBoolean(Preferences.PREF_AUTO_UPDATE, false);

Resources r = getResources();

int[] minMagValues = r.getIntArray(R.array.magnitude);

int[] freqValues = r.getIntArray(R.array.update_freq_values);

minimumMagnitude = minMagValues[minMagIndex];

int updateFreq = freqValues[freqIndex];

if (autoUpdate) {

int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;

long timeToRefresh = SystemClock.elapsedRealtime() + updateFreq*60*1000; alarms.set(alarmType, timeToRefresh, alarmIntent);






You can now remove the updateTimer instance variable and the TimerTask instance doRefresh.


Services are one of the most compelling reasons to develop applications on the Android platform. In this chapter, you learned how to use these invisible application components to perform processing while your applications are hidden in the background.

You were introduced to Toasts, a transient message box that lets you display information to users with-out stealing focus or interrupting their workflow.

You used the Notification Manager to send alerts to your users from within Services and Activities using customized LEDs, vibration patterns, and audio files to convey detailed event information.

Using Alarms, you were able to preset events and actions on the device using Intents to broadcast actions or start Activities or Services.

This chapter also demonstrated how to:

❑Bind a Service to an Activity to make use of a more detailed, structured interface.

❑Ensure that your applications remain responsive by moving time-consuming processing like network lookups onto worker threads.

❑Use handlers to synchronize child threads with the main application GUI when performing operations using visual controls and Toasts.

❑Create insistent and ongoing Notifications.

In Chapter 9, you’ll be introduced to the communications features of Android. Starting with a look at the GTalk Service, you’ll learn how to send and receive text and data messages to transmit data between devices. You’ll then investigate the SMS functionality available for you to send and receive SMS text and data messages.