Android Shutdown Alarm

Source: Internet
Author: User

The new mobile phone requires an alert clock in the shutdown state. When I first started to access android, I wondered why android does not support some features in the shutdown state? Even though the drivers on each platform are different, the upper-layer applications can provide unified interfaces. In fact, they support shutdown and charging at 4.0, the shutdown alarm is not supported by default. Many brand mobile phones on the market do not support this function, making it difficult for many users who use Feature phones and worry about radiation. This Shutdown Alarm refers to the implementation of shutdown and charging based on some ideas. The overall idea is as follows: Use PMU in uboot to determine the reason for boot. If the RTC module enables boot, the startup parameter androidboot will be passed in uboot. mode = alarm, and then judge the Startup mode in the init process (the current system has three startup modes: normal, charger, and alarm). If it is alarm mode, the alarm service is started, the alarm service is associated with the application alarm. Therefore, you need to write an application to implement the function of shutting down the alarm. Applications mainly implement the following functions: 1. display the UI and current time of the Shutdown Alarm; 2. Play the alarm; 3. Read the input event to determine user operations; 4. You can select the lazy mode, Boot, or shutdown mode in the UI. The following describes the implementation of these functions one by one: 1. For details about how to display the UI before zygote is started, refer to charger's practice and use the minui interface of android, these interfaces provide graphical representations and fixed-size text display. The function is described as follows: [cpp] int gr_init (void);/* initializes the graphic display, it is mainly used to open the device, allocate memory, initialize some parameters */void gr_exit (void);/* log out of the graphic display, close the device and release the memory */int gr_fb_width (void ); /* obtain the screen width */int gr_fb_height (void);/* obtain the screen height */gr_pixel * gr_fb_data (void ); /* obtain the cache address of the displayed data */void gr_flip (void);/* refresh the display content */void gr_fb_blank (bool blank );/* Clear screen */void gr_color (unsigned char r, unsigned char g, unsigned char B, unsigned char a);/* set the font color */void gr_fill (int x, int y, int w, int h);/* fill the rectangular area with parameters representing the starting coordinate and the size of the rectangular area */int gr_text (int x, int y, const char * s ); /* display string */int gr_measure (const char * s);/* obtain the pixel length occupied by the string in the default font */void gr_font_size (int * x, int * y);/* Get the length and width of a character in the current font */void gr_b133 (gr_surface source, int sx, int sy, int w, int H, int dx, int dy);/* fill in the image specified by source */unsigned int gr_get_width (gr_surface surface ); /* Get the image width */unsigned int gr_get_height (gr_surface surface);/* Get the image height * // * Create display resource data based on the image, name is the relative path */int res_create_surface (const char * name, gr_surface * pSurface) specified in the mk file; void res_free_surface (gr_surface surface ); /* release resource data */images only support the png format. It takes a lot of time to create image resources for this UI (no artist has been done). Generally, resource data is created by res_create_surface for the display of images, then tune Fill with gr_bp and call gr_flip to refresh the display. In the Shutdown Alarm interface also need to display the current time, the first call minui default font to display, but the default font is too small, only supports 10x18 ASIC-II encoding characters, the effect was not good. Later, the 10 digits and symbols required by the time were displayed as images. 2. At this stage, only tinyplay can be used for playing an alarm. tinyplay is a simple player that comes with android. It can only play wav files in fixed formats. The UI display and playing alarm events are exclusively owned by one thread to ensure that they are not disturbed. 3. input evnt when the alarm clock starts to ring, you can click the touch screen to select whether to switch the host or enter the lazy mode. Here, you need to determine the user operation, that is, read the data of the device under/dev/input in the program. When you enter the lazy mode, the alarm will stop for 5 minutes and then ring. At this stage, you need to turn off the LCD and touch screen. You can press the button to wake up the LCD. The input event is read and processed cyclically in the process. The sample code is as follows: [cpp] static int event_loop (void) {int I; int ret = 0; int nfds = ALARM_MAX_DEVICE; struct input_event event; const char * device = NULL; const char * device_path = "/dev/input"; ret = scan_dir (device_path);/* scan the device nodes in the directory, only the touch screen and buttons */if (ret <0) {printf ("scan dir failed for % s. \ n ", device_path); return ret;} for (;) {poll (ufds, nfds,-1);/* Check for touch screen or button events by polling * /For (I = 0; I <nfds; I ++) {if (ufds [I]. revents) {/* have valid value. */if (ufds [I]. revents & POLLIN) {ret = read (ufds [I]. fd, & event, sizeof (event);/* read event */if (ret <(int) sizeof (event) {printf ("cocould not get event. \ n "); continue;} handle_event (event. type, event. code, event. value);/* handle events */} return 0;} Appendix: permission management mechanism for android encountered a permission issue during compilation and use of alarm: the compiled alarm executable program is in the out directory If you have the executable permission, but you find that you do not have the executable permission after installing the machine, you finally find that the permission management mechanism of android is caused.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.