Handling alarms with BroadcastReceiver
We met BroadcastReceiver
already in Chapter 5, Interacting with Services, where we used it in an Activity
to receive broadcasts from a Service
. In this section, we'll use BroadcastReceiver
to handle alarms set on the AlarmManager
.
BroadcastReceivers
can be registered and unregistered dynamically at runtime like we did in Chapter 5, Interacting with Services, with Service
, or statically in the Android manifest file with a <receiver>
element, and can receive alarms regardless of how they are registered.
It is more common to use a statically registered receiver for alarms, because these are known to the system and can be invoked by alarms to start an application if it is not currently running.
Let's implement a static defined BroadcastReceiver
that is able to dispatch an SMS to a phone number when an alarm sounds. First we will define our BroadcastReceiver
in the manifest file:
<receiver android:name=".chapter6.SMSDispacther"> <intent-filter...