Android Apidemos Sample resolution (m) App->alarm->alarm Service

Source: Internet
Author: User

The Alarm service and the Alarm Controller example are very similar, except that the Alarm service is used to schedule a service, and the previous example is to schedule a broadcast.

Previously said Pendingintent, can describe an activity, broadcast, or a service. This example schedule a alarm event to start a service. This is typically used to perform a more time-consuming task.

If writing a service will be followed by a special example to illustrate, only not in detail. As long as you know Alarmservice_service is a service on the line.

The following code is used to schedule a multiple alarm event to start Alarmservice_service

Private Pendingintent Malarmsender;     
 ...     
Create a intentsender that would launch our service, to be scheduled     
 //with the alarm manager.     
Malarmsender = Pendingintent.getservice (Alarmservice.this,     
 0, New Intent (Alarmservice.this, Alarmservice_ Service.class), 0);     
...     
We want the "alarm to go" seconds from now.     
Long firsttime = Systemclock.elapsedrealtime ();     
          
Schedule the alarm!     
Alarmmanager am = (alarmmanager) getsystemservice (alarm_service);     
Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup,     
 firsttime, 30*1000, Malarmsender);

Cancel this alarm event:

and cancel the alarm.     
Alarmmanager am = (alarmmanager) getsystemservice (alarm_service);     
Am.cancel (Malarmsender);

Code is similar to Alaram controller, the same method can schedule a alarm event to trigger an activity.

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.