A simple Android timed task

Alarm manager is used to realize timing reminder, which provides a way to access the system alarm clock service. In the global, when the set time is reached, an intent registered on it will be broadcast, and then start the corresponding application, which can be realized by customizing the receiver.

First, register in androidmanifest. XML

Customize an alarm receiver. Getintextra can be used to accept the value sent by the sender putextra. Intent I = new intent ("clock"), where "clock" is the name of the secreceiver used during registration.

The time is obtained through DatePickerDialog and timepickerdialog. It should be noted that the month of DatePickerDialog starts from 0. Put the obtained data in the calendar class, where the month is also stored from 0.

Set a pendingintent object as the object to respond to the alarm clock and send a broadcast. The second parameter can be 0, but because it is a one-time alarm clock, if it is 0, the newly set alarm clock will overwrite the previously set alarm clock. FLAG_ UPDATE_ Current: if the pendingintent object you want to obtain is different from the existing pendingintent object, if only the data attached by the intent is different, the existing pendingintent object will not be cancelled, but the data attached by the new intent will be reloaded.

PendingIntent pi = PendingIntent.getBroadcast(this,_id,i,PendingIntent.FLAG_UPDATE_CURRENT);

Send an alarm request and set the alarmcalendar. Gettimeinmillis() time to start the component specified by PI. The first parameter is used to specify the type of scheduled service. The following values are available:

AlarmManager.ELAPSED_ Realtime: not available in sleep state. In this state, the alarm clock uses relative time. AlarmManager.ELAPSED_ REALTIME_ Wakeup: wakes up the system and performs the prompt function in the sleep state, and also uses the relative time. Alarmmanager.rtc: this state is not available in sleep state. Absolute time is used, that is, the current system time. AlarmManager.RTC_ Wakeup: the alarm clock will wake up the system and perform the prompt function in the sleep state. In this state, the alarm clock also uses the absolute time.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>