Android AlarmManager alarm clock implementation

Source: Internet
Author: User

Android AlarmManager alarm clock implementation

 

What is AlarmManager? AlarmManager is a system-level prompt service commonly used in Android. It broadcasts a specified Intent for us at a specific time. In short, we set a time, and then when the time comes, AlarmManager broadcasts a set Intent for us. Generally, we use PendingIntent, which can be understood as an Intent package, simply put, a specified action is added to the Intent. When using Intent, we also need to execute startActivity, startService or sendBroadcast to make Intent useful. PendingIntent includes this action.
Define a PendingIntent object PendingIntent pi = PendingIntent. getBroadcast (this, 0, intent, 0 );
Common API1 and set (int type, long startTime, PendingIntent pi) of AlarmManager are used to set a one-time alarm. The first parameter indicates the alarm type, and the second parameter indicates the alarm execution time, the third parameter indicates the alarm response action.
2. setRepeating (int type, long startTime, long intervalTime, PendingIntent pi) This method is used to set the recurrence alarm. The first parameter indicates the alarm type, and 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 response of the alarm.
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.
Parameter description: Specifies the int type alarm type. Five values are commonly used: AlarmManager. ELAPSED_REALTIME, AlarmManager. Timeout, AlarmManager. RTC, AlarmManager. RTC_WAKEUP, and AlarmManager. POWER_OFF_WAKEUP.
LarmManager. ELAPSED_REALTIME indicates that the alarm is unavailable when the mobile phone is sleeping. In this status, the relative time of the alarm is used (relative to the start of the system), 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 relative time, And 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 alarm uses the absolute time 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;
The first execution time of the long startTime 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_R EALTIME_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 ().
Long intervalTime for the last two methods, this attribute exists, indicating the interval between two alarm clock executions, also in milliseconds.
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.

Write a simple Demo

 

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create an Intent object. The action is ELITOR_CLOCK, and the additional information is the string "You Should soy sauce" Intent intent = new Intent (ELITOR_CLOCK); intent. putExtra (msg, You should have soy sauce); // defines a PendingIntent object, PendingIntent. getBroadcast contains the sendBroadcast action. // That is, intent PendingIntent pi = PendingIntent that sends the action as ELITOR_CLOCK. getBroadcast (this, 0, intent, 0); // AlarmManager object. Note that this is not a new object. Alarmmanager is a system-level service AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE ); // set the PendingIntent object pi every 5s from the current time. Pay attention to the relationship between the first parameter and the second parameter. // broadcast am is sent through the PendingIntent pi object after 5 seconds. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis (), 5*1000, pi );}}

 

Broadcast receiver: MyReceiver. java

public class MyReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // TODO Auto-generated method stub        Log.d(MyTag, onclock......................);        String msg = intent.getStringExtra(msg);        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();    }}
The configuration file is as follows: AndroidMainfest. xml

 

 
                         
                                  
               
   
                  
              
                               
           
      
 
Running result:

 

 

 

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.