Use of alarmmanager

Source: Internet
Author: User

Generally, we use timer and alarmmanager for scheduled tasks.

Let's briefly describe how to use Timer:

1 timertask task = new timertask () {2 Public void run () {3 message = new message (); 4 message. what = 1; 5 handler. sendmessage (Message); 6} 7}; 8 timer = new timer (true); 9 timer. schedule (task, 1000,100 0); // The task is executed after a delay of MS, and 10 timer is executed once within Ms. cancel (); // exit the timer

It's easy. Timer is not the focus. We will focus on how alarmmanager implements scheduled tasks.
First, let's talk about the key functions of the alarmmanager class.

Setrepeating (INT type, long starttime, long intervaltime, pendingintent PI) is used to set the repeated alarm.

First parameter:
Elapsed_realtime: the alarm clock is not available in sleep state and is relative to the system start time.

Elapsed_realtime_wakeup: the alarm clock is available in sleep state, which is relative to the system start time.

RTC: the alarm clock is unavailable during sleep and uses real time.

Rtc_wakeup: the alarm clock is available during sleep, and the real time is used. * This is generally used *

The second parameter is the delay time, that is, the delay time for running the first scheduled task.

Third parameter: The task interval after the first parameter;

The fourth is the literal meaning of pendingintent: waiting, undecided intent. The task to be executed, whether it is a service or activity.

My usage is as follows:

1 AlarmManager alarm=(AlarmManager)getSystemService(ALARM_SERVICE);2 Intent intent =new Intent(UplBasicParams.this, AlarmReceiver.class);3 intent.setAction(GETADTASKACTION);4 sender=PendingIntent.getBroadcast(UplBasicParams.this, 0, intent, 0);5 alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),60*1000, sender);

Sometimes we need to reset the scheduled task, so the sender must use the same one.
Row 4th getbroadcast uses this method to obtain the pendingintent because it calls a receiver. If you want to call a service, you need to useGetserviceTo obtain pi

Well, let's talk about it here. If you have any technical questions, please contact QQ Group 263862916.

Use of 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.