2016年2月27日 星期六

Android Alarm

Using Alarms

alarm manager allow:
wake up a component of the application after period of time,and do some processing in the background

what we wake up in the background??
That’s brodcast receiver.
it receive intent brodcast from opther application.
it registe to the intent filter ,to decide what it brodcast it receive.
So,it can listen to the alarm.

need to use pending intent

it is a special intent form one application to other appplication.

alarm manager use pending intent to talk to the brodcast receiver we create

Now,we want Start the code in AlarmReceiver five seconds after updateWeather is called (for testing purposes this time is short).

  • Start the code in AlarmReceiver five seconds after updateWeather is called (for testing purposes this time is short).
  • It should only fire once.
  • It should wake up the phone if it is asleep.



How to use a brodcast receiver?
Step1
create a public static inner class Brodcast receiver inner class in the Intent service Class and implement onReceive. The onReceive will be call when Broadcast Intent is fired.

step2
register it in the AndroidManifest

<receiver android:name=".service.SunshineService$AlarmReceiver" android:enabled="true"/>


step3
now we select the alarm type
  • ELAPSED_REALTIME—Fires the pending intent based on the amount of time since the device was booted, but doesn't wake up the device. The elapsed time includes any time during which the device was asleep.
  • ELAPSED_REALTIME_WAKEUP—Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.
  • RTC—Fires the pending intent at the specified time but does not wake up the device.
  • RTC_WAKEUP—Wakes up the device to fire the pending intent at the specified time.

It is because we want to send the intent after 5second when the updateweather start and need wake up the device,not a specific clock time,so we choose ELAPSED_REALTIME_WAKEUP.


below talk about how to choose:

Choose an alarm type
One of the first considerations in using a repeating alarm is what its type should be.
There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.
Both types have a "wakeup" version, which says to wake up the device's CPU if the screen is off. This ensures that the alarm will fire at the scheduled time. This is useful if your app has a time dependency—for example, if it has a limited window to perform a particular operation. If you don't use the wakeup version of your alarm type, then all the repeating alarms will fire when your device is next awake.
If you simply need your alarm to fire at a particular interval (for example, every half hour), use one of the elapsed real time types. In general, this is the better choice.
If you need your alarm to fire at a particular time of day, then choose one of the clock-based real time clock types. Note, however, that this approach can have some drawbacks—the app may not translate well to other locales, and if the user changes the device's time setting, it could cause unexpected behavior in your app. Using a real time clock alarm type also does not scale well, as discussed above. We recommend that you use a "elapsed real time" alarm if you can.

Here is the list of types:
  • ELAPSED_REALTIME—Fires the pending intent based on the amount of time since the device was booted, but doesn't wake up the device. The elapsed time includes any time during which the device was asleep.
  • ELAPSED_REALTIME_WAKEUP—Wakes up the device and fires the pending intent after the specified length of time has elapsed since device boot.
  • RTC—Fires the pending intent at the specified time but does not wake up the device.
  • RTC_WAKEUP—Wakes up the device to fire the pending intent at the specified time.

step4
choose the trigger time. If the trigger time you specify is in the past, the alarm triggers immediately.is the now time+5 second

step5


The alarm's interval. For example, once a day, every hour, every 5 seconds, and so on.
now we no need interval,becuase we only send the padding intent once time.

step6
  • A pending intent that fires when the alarm is triggered. When you set a second alarm that uses the same pending intent, it replaces the original alarm.
now we can start to code

step7 create a alarm manager
AlarmManager alarmMgr = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

step8
create the intent to trig the Brodcast receiver
Intent intent = new Intent(getActivity(), SunshineService.AlarmReceiver.class);

step9
because the alarm manager nee padding intent,so make the padding intent from the intent
PendingIntent pendintent = PendingIntent.getBroadcast(getActivity(), 0, intent, 0);
step10
set the alarm manager to send the padding intent after 5 second
ELAPSED_REALTIME_WAKEUP

alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5*1000,pendintent);

more information see: