Android Timer Alarmmanager

Source: Internet
Author: User

1, Android usually use Alarmmanager to start a single or repeated multiple operations at a time. Specifically, we set a time through Alarmmanager and register a intent to the system, and then when that time comes, the system sends us a broadcast that executes the intent we set (the action to be performed), and usually we use Pendingintent to implement "What to do", pendingintent can be understood as the intent package, which is simply the intent action to be performed in the future. The difference is that when using intent, we also need to perform startactivity, StartService or sendbroadcast in order to make intent useful, and pendingintent is to include this action, can be executed directly.

Defines a Pendingintent object.

        Start service        Intent Intent = new Intent (instance, recommendservice.class);//Create a new Intent        that can perform the current context operation Intent.putextra ("Userinfobody", userinfobody);        Intent.putextra ("IsDynamic", true);        Intent.putextra ("content", comment);        if (! Textutils.isempty (Sayhiorcollect)) {            Intent.putextra ("Sayhiorcollect", Sayhiorcollect);        }        Pendingintent sender = Pendingintent.getservice (instance, 1, intent, pendingintent.flag_update_current);// The first parameter is the context, the second parameter is the difference between different intent code, the third parameter is intent, the fourth is sent two the same
Pendingintent how to deal with the data and intent, see the parameters described below
        Long firsttime = Systemclock.elapsedrealtime ();        Firsttime + = 1 * +;        Schedule the alarm! * * *        alarmmanager AM = (alarmmanager) getsystemservice (alarm_service);        Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime, Ten * (), sender),//alarmmanager parameter meaning see the instructions below

Note: Two pendingintent are the same as their operation, and their intent action, data, categories, components, and flags are the same. But the extra of their intent can be different. As a result, the intent carry content can not be updated, the solution is to set the Pendingintent.flag_update_current

The main constants of the last parameter of the GetService () method in the code are:

Flag_cancel_current: If an identical pendingintent object already exists in the current system, the existing pendingintent will be canceled before a Pendingintent object is regenerated. Flag_no_create: If the same Pendingintent object does not exist in the current system, the system will not create the Pendingintent object and return null directly. Flag_one_shot: The pendingintent only works once. After the Pendingintent object is triggered by the Send () method, Pendingintent will automatically call cancel () to destroy, and if you call the Send () method again, the system will return a sendintentexception. Flag_update_current: If there is a pendingintent equivalent pendinginent that you describe in the system, then the Pendingintent object will be used by the system. However, the new intent is used to update the intent object data in the previous pendingintent, such as extras in the update intent.

2, there are three common methods of Alarmmanager:

(1) Set (int type,long starttime,pendingintent pi);

This method is used to set a one-time alarm, the first parameter represents the alarm type, the second parameter indicates the alarm execution times, and the third parameter represents the alarm response action.

(2) setrepeating (int type,long starttime,long intervaltime,pendingintent pi);

This method is used to set a repeating alarm, the first parameter represents the alarm type, the second parameter represents the first time the alarm is executed, the third parameter represents the interval between two executions of the alarm, and the third parameter represents the alarm response action.

(3) setinexactrepeating (int type,long starttime,long intervaltime,pendingintent pi);

This method is also used to set a repeating alarm, similar to the second method, although the interval between the two alarms is not fixed.

3, three methods of each parameter keyholder:

(1) INT type: Alarm types, commonly used with 5 values: Alarmmanager.elapsed_realtime, Alarmmanager.elapsed_realtime_wakeup, ALARMMANAGER.RTC, Alarmmanager.rtc_wakeup, Alarmmanager.power_off_wakeup.

Alarmmanager.elapsed_realtime means that the alarm clock is not available when the phone is asleep, and the alarm is used relative to the time (starting with the system boot) and the state value is 3;

Alarmmanager.elapsed_realtime_wakeup indicates that the alarm will wake up the system and perform the prompt function, the alarm clock also uses relative time, the status value is 2;

ALARMMANAGER.RTC indicates that the alarm is not available in sleep, and the alarm is used in absolute time, that is, the current system time, the status value is 1;

Alarmmanager.rtc_wakeup means that the alarm will wake up the system and perform the prompt function when the alarm is in the state of sleep, which has an absolute time and a state value of 0.

Alarmmanager.power_off_wakeup means that the alarm clock in the state of the hand machine can also be a normal prompt function, so it is one of the most used in 5 states, the alarm is also used absolute time, the status value of 4; But this state seems to be affected by the SDK version, Some editions are not supported;

(2) long startTime: The first time the alarm is executed, in milliseconds, the time can be customized, but the current time is generally used. It is important to note that this property is closely related to the first property (type), and if the alarm corresponding to the first parameter uses a relative time (Elapsed_realtime and Elapsed_realtime_wakeup), then this property will have to use relative time (relative to System start-up time), such as the current time is expressed as: Systemclock.elapsedrealtime (); If the first parameter corresponds to an alarm that uses an absolute time (RTC, Rtc_wakeup, Power_off_wakeup), Then this property will have to use absolute time, such as the current time is expressed as: System.currenttimemillis ().

(3) long intervaltime: For the latter two methods, this property exists, indicating the interval between two alarm executions, and in milliseconds.

(4) Pendingintent pi: Binding the execution of the alarm clock, such as sending a broadcast, giving hints and so on. Pendingintent is the encapsulation class for intent. It should be noted that if the alarm is implemented by starting the service, the Pendingintent object should be acquired using the Pending.getservice (Context c,int i,intent intent,int j) method If the alarm is realized by broadcasting, the Pendingintent object should be acquired using Pendingintent.getbroadcast (Context c,int i,intent intent,int j) method. If the alarm is implemented in an activity way, the Pendingintent object should be acquired using the pendingintent.getactivity (Context c,int i,intent intent,int j) method. If these three methods are wrong, although not error, but do not see the alarm effect.

4. In the above code

Recommendservice.class
The specific contents of the class:
public class Recommendservice extends Intentservice  {    @Override    protected void onhandleintent (Intent Intent) {                }}    

The advantage of the intentservice is that the intent operations that are sent by the timer are executed several times in sequence, and the time-consuming intent operation is not created on the new child thread, because Intentsevice has established intent for each workthread.

Android Timer Alarmmanager

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.