In this post I will describe the architecture of one application - "Do not disturb" (similar to the iOS6 feature with the same name ) . It appears a simple one, but turns out to be fairly complex from Architecture stand-point.
UX:The app is a custom alarm-clock, during the time it runs, incoming calls and notifications are silenced, unless you call twice in 3 minutes. An sms saying that the call was silenced is sent to the caller.
Implementation:The straightforward part is an activity which provide the GUI to set the hour and a button to click "start". Upon clicking either start or stop, an intent is sent to a service which handle it.
The service handle the start command by registering an alarm in the alarm-manager to the designated time. It also becomes foreground-service to add a status-bar notification, an icon, of alarm-clock is set, and it stays active without any cpu usage, but will (almost) guranteed to not get killed.
The service handle the stop command by canceling the alarm in the alarm-manager.
On the service sits a broadcast receiver for telephony, which will count the number of calls of each number, send the automatic SMS if needed , and un-silence the phone if needed.
To cope with a case of device-restart (user-restart or just software/hardware bug) we add a broadcast receiver for BOOT_COMPLETED and ACTION_EXTERNAL_APPLICATIONS_AVAILABLE, which will re-start the service and test if an alarm is active.