Ongoing and Insistent Notifications

You can configure Notifications as ongoing and/or insistent by setting the flag_insistent and FLAG_ONGOING_EVENT flags.

Notifications flagged as ongoing, as in the following snippet, are used to represent events that are currently in progress (such as a download in progress or music playing in the background). An ongoing Notification is a requirement for a foreground Service, as described earlier in this chapter.

notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

Ongoing events are separated from regular Notifications within the extended Notification drawer, as shown in Figure 9-6.

Insistent Notifications repeat their audio, vibration, and light settings continuously until canceled. These Notifications are typically used for events that require immediate and timely attention —such as an incoming call or the ringing of an alarm clock.

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.

The following code snippet shows how to set a Notification as insistent:

notification.flags = notification.flags | Notification.FLAG_INSISTENT;

USING ALARMS

Alarms are an application-independent means of firing Intents at predetermined times and intervals.

Alarms are set outside the scope of your applications, so they can be used to trigger application events or actions even after your application has been closed. They can be particularly powerful in combination with Broadcast Receivers, enabling you to set Alarms that fire broadcast Intents, start Services, or even open Activities, without the applications' needing to be open or running until they're required.

As such, Alarms are an extremely effective means of reducing your application's resource requirements, particularly when running in the background, by enabling you to stop Services and eliminate timers while maintaining the ability to perform scheduled actions.

For example, you can use Alarms to implement an alarm clock application, perform regular network lookups, or schedule time-consuming or cost-bound operations at ''off-peak'' times.

For timing operations that occur only during the lifetime of your applications, using the Handler class in combination with Timers and Threads is a better approach than using Alarms, as this allows Android better control over system resources. Alarms provide a mechanism to reduce the lifetime of your applications by moving scheduled events out of their control.

Alarms in Android remain active while the device is in sleep mode and can optionally be set to wake the device; however, all Alarms are canceled whenever the device is rebooted.

Alarm operations are handled through the AlarmManager, a system Service accessed via getSystemService, as shown here:

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

To create a new one-shot Alarm, use the set method and specify an alarm type, a trigger time, and a Pending Intent to fire when the Alarm triggers. If the trigger time you specify for the Alarm occurs in the past, the Alarm will be triggered immediately.

FIGURE 9-6

There are four alarm types available. Your selection will determine if the time value passed in the set method represents a specific time or an elapsed wait:

> rtc_wakeup Wake the device from sleep to fire the Pending Intent at the clock time specified.

> rtc Fire the Pending Intent at the time specified, but do not wake the device.

> elapsed_realtime Fire the Pending Intent based on the amount of time elapsed since the device was booted, but do not wake the device. The elapsed time includes any period of time the device was asleep. Note that the time elapsed is calculated based on when the device was last booted.

> elapsed_realtime_wakeup After a specified length of time has passed since device boot, wake the device from sleep and fire the Pending Intent.

The Alarm-creation process is shown in Listing 9-25.

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