Android timer usage

Source: Internet
Author: User

Android timer usage
1. In android, AlarmManager is usually used to start one or more operations at a time. Specifically, we use AlarmManager to set a time and register an intent to the system. Then, when the time arrives, the system sends a broadcast to us, that is, execute the set Intent (the operation to be executed). Usually we use PendingIntent to implement the "operation to be executed". PendingIntent can be understood as an Intent encapsulation package, in short, it is the Intent operation to be executed in the future. Their difference is: when using Intent, we also need to execute startActivity, startService, or sendBroadcast to make Intent useful, while PendingIntent includes this action and can be executed directly. Defines a PendingIntent object.

// Start service Intent intent = new Intent (instance, RecommendService. class); // create an Intent intent that can execute the current context operation. 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 context, the second parameter is the difference code to distinguish different intent, and the third parameter is intent, the fourth is how to process data and intent when two identical // PendingIntent messages are sent. For details, see the following parameter introduction long firstTime = SystemClock. elapsedRealtime (); firstTime + = 1*1000; // Schedule the alarm! 16*60*1000 AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE); am. setRepeating (AlarmManager. ELAPSED_REALTIME_WAKEUP, firstTime, 10*60*1000, sender); // The meanings of AlarmManager parameters are described below

 

Note: Two pendingintents have the same operation, and their Intent actions, data, categories, components, and flags are the same. However, their Intent Extra can be different. Therefore, the content carried by intent cannot be updated. The solution is to set PendingIntent. in the FLAG_UPDATE_CURRENT code, the main constant of the last parameter of the getService () method is FLAG_CANCEL_CURRENT. If the current system already has the same PendingIntent object, the existing PendingIntent is canceled first, then generate a new PendingIntent object. FLAG_NO_CREATE: if the same PendingIntent object does not exist in the current system, the system will not create this PendingIntent object, but will return null directly. FLAG_ONE_SHOT: This PendingIntent only works once. After the PendingIntent object is triggered by the send () method, PendingIntent will automatically call cancel () for destruction. If you call the send () method again, the system will return a SendIntentException. FLAG_UPDATE_CURRENT: if the system has a PendingInent equivalent to the PendingIntent you described, the system uses the PendingIntent object, but uses the new Intent to update the Intent object data in the previous PendingIntent, for example, update Extras in Intent. 2. There are three common methods for AlarmManager: (1) set (int type, long startTime, PendingIntent pi). This method is used to set a one-time alarm. The first parameter indicates the alarm type, the second parameter indicates the execution time of the alarm, and the third parameter indicates the response action of the alarm. (2) setRepeating (int type, long startTime, long intervalTime, PendingIntent pi); this method is used to set the repeated alarm. The first parameter indicates the alarm type, the second parameter indicates the first execution time of the alarm, the third parameter indicates the interval between two executions of the alarm, and the third parameter indicates the alarm response action. (3) setInexactRepeating (int type, long startTime, long intervalTime, PendingIntent pi); this method is also used to set repeated alarms, similar to the second method, however, the interval between the two alarm clocks is not fixed. 3. Parameters of the three methods are described in detail: (1) int type: the type of the alarm, commonly used five values: AlarmManager. ELAPSED_REALTIME, AlarmManager. ELAPSED_REALTIME_WAKEUP and AlarmManager. RTC, AlarmManager. RTC_WAKEUP and AlarmManager. POWER_OFF_WAKEUP. AlarmManager. ELAPSED_REALTIME indicates that the alarm is unavailable when the mobile phone is sleeping. In this status, the relative time (relative to the start of the system) of the alarm is used, and the status value is 3; AlarmManager. ELAPSED_REALTIME_WAKEUP indicates that the alarm will wake up the system during sleep and execute the prompt function. In this status, the alarm will also use the relative time. The status value is 2; AlarmManager. RTC indicates that the alarm is unavailable during sleep. In this status, the absolute time is used for the alarm, that is, the current system time. The status value is 1; AlarmManager. RTC_WAKEUP indicates that the alarm will wake up the system during sleep and execute the prompt function. In this status, the absolute time is used for the alarm, and the status value is 0; AlarmManager. POWER_OFF_WAKEUP indicates that the alarm notification function can be performed normally when the phone is shut down. Therefore, it is one of the five most frequently used states. In this status, the alarm notification uses absolute time and the status value is 4; however, this status seems to be affected by the SDK version, which is not supported by some versions; (2) l Ong startTime: the first execution time of the alarm, in milliseconds. The time can be customized, but the current time is generally used. Note that this attribute is closely related to the first attribute (type). If the first parameter uses the relative time (ELAPSED_REALTIME and ELAPSED_REALTIME_WAKEUP) for the alarm ), this attribute uses the relative time (relative to the system startup time). For example, the current time is represented as SystemClock. elapsedRealtime (); if the alarm clock corresponding to the first parameter uses absolute time (RTC, RTC_WAKEUP, POWER_OFF_WAKEUP), this attribute uses absolute time. For example, the current time is expressed: system. currentTimeMillis (). (3) long intervalTime: For the last two methods, this attribute exists, indicating the interval between the two alarms, in milliseconds. (4) PendingIntent pi: it is bound to the execution action of the alarm, such as sending a broadcast and giving a prompt. PendingIntent is the encapsulation class of Intent. It should be noted that if the alarm is triggered by starting the service, the PendingIntent object should be obtained using Pending. getService (Context c, int I, Intent intent, int j) method; if the alarm is triggered through broadcast, the PendingIntent object should be obtained using PendingIntent. getBroadcast (Context c, int I, Intent intent, int j) method; if the alarm is triggered by an Activity, the PendingIntent object should be obtained by PendingIntent. getActivity (Context c, int I, Intent intent, int j) method. If these three methods are used incorrectly, although no error is reported, the alarm is not displayed. 4. the specific content of the RecommendService. class in the code above:
public class RecommendService extends IntentService  {    @Override    protected void onHandleIntent(Intent intent) {                }}    

 

The advantage of the intentService is that the intent operation sent by the timer is executed in sequence for multiple times, and the time-consuming intent operation does not need to be performed in the new sub-thread, because intentSevice has created workThread for each intent.

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.