Use of *android alarm clock Alarmmanager

Source: Internet
Author: User

Alarmmanager Introduction

  Alarmmanager This class provides access to the system alarm service.

You can set a feature to wake up at some time in the future for your app.

When the alarm goes off, the system actually sends out a broadcast that is registered for the alarm clock and automatically opens the target app.

The registered alarm clock is still retained when the device is asleep, optionally setting whether to wake the device, but the alarm will be cleared when the device is turned off and restarted.

When the Alarm receiver's onreceive () method is executed, the Alarm manager holds a wake-on-CPU lock , which ensures that the device does not sleep until the broadcast has finished processing.

Once the OnReceive () method returns, Alarm manager releases the lock, indicating that in some cases the OnReceive () method will sleep when it finishes executing the device.

If you call Context.startservice () in your alarm receiver, it is likely that the service is not up and the device will sleep.

To prevent this, your broadcastreceiver and service need to implement different wake-up locking mechanisms to ensure that the device continues to operate until the service is available.

  Note : Alarm Manager is primarily used to run your code at specific times, even if your app does not run at that particular moment.

  For conventional timing operations (ticks, timeouts, etc), it is more convenient and efficient to use handler processing.

Another: starting with API 19, the alarm mechanism is non-accurate , and the operating system will convert the alarm to minimize wake-up and battery usage.

There are some new APIs that support strictly accurate delivery, see SetWindow (int, long, long, pendingintent) and setexact (int, long, pendingintent).

  Targetsdkversion applications will continue to use previous behavior before API 19 , and all alarms will be delivered accurately in the case of accurate delivery.

Alarm Clock Demo

In Android Api demos, there is a demo for the use of alarms:

  Com.example.android.apis.app.AlarmController

There are two kinds of alarm clocks, one is disposable and one is repetitive.

Declaration in manifest, process property

The custom receiver, declared in manifest, is as follows:

        <receiver            android:name= ". Oneshotalarm "            android:process=": Remote "/>        <receiver            android:name=". Repeatingalarm "            android:process=": Remote "/>

The two receiver OnReceive methods in the demo show their toast prompts, so they are no longer listed.

Discuss the process property here, which specifies the processes in which the component (activity, service, receiver, etc.) resides.

Typically, this attribute is not specified, and all components of an app run in the app's default process, with the name of the process consistent with the app's package name.

such as Manifest's package= "Com.example.helloalarm", the default process name is Com.example.helloalarm.

  The process property of the <application> element can set a different default process for all components.

The component can override this default process setting so that your application can be multi-process.

If your process property starts with a colon , the procedure name appends a colon-based string after the original process name as the new process name. This process is created automatically when the component needs it. This process is an application-private process.

If the process attribute starts with a lowercase letter , it will be directly in the name of the property, which is a global process that can be shared by components in multiple different applications.

Disposable Alarm Clock

            When the alarm goes off, we want to broadcast a Intent to our//broadcastreceiver.            Here we make a Intent with a explicit class//name to the own receiver (which had been published in Androidmanifest.xml) instantiated and called, and then create a//Intentsender to the intent            Executed as a broadcast.            Intent Intent = new Intent (alarmcontroller.this, Oneshotalarm.class);            Pendingintent sender = Pendingintent.getbroadcast (alarmcontroller.this, 0, intent, 0);            We want the alarm to go off ten seconds from now.            Calendar calendar = Calendar.getinstance ();            Calendar.settimeinmillis (System.currenttimemillis ());            Calendar.add (Calendar.second, 10);            Schedule the alarm!            Alarmmanager am = (alarmmanager) getsystemservice (Alarm_service); Am.set (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), sender);

Repeat Alarm

Alarm settings:

            When the alarm goes off, we want to broadcast a Intent to our//broadcastreceiver.            Here we make a Intent with a explicit class//name to the own receiver (which had been published in Androidmanifest.xml) instantiated and called, and then create a//Intentsender to the intent            Executed as a broadcast.            Note that unlike above, this intentsender is configured to//allow itself to be sent multiple times.            Intent Intent = new Intent (alarmcontroller.this, Repeatingalarm.class);            Pendingintent sender = Pendingintent.getbroadcast (alarmcontroller.this, 0, intent, 0);            We want the alarm to go off ten seconds from now.            Calendar calendar = Calendar.getinstance ();            Calendar.settimeinmillis (System.currenttimemillis ());            Calendar.add (Calendar.second, 10);           Schedule the alarm! Alarmmanager am = (alarmmanager) getsystemservice (Alarm_service); Am.setrepeating (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (), ten *, sender);

Alarm Cancel:

            Create the same intent, and thus a matching intentsender, for            //The one is scheduled.            Intent Intent = new Intent (alarmcontroller.this,                    repeatingalarm.class);            Pendingintent sender = Pendingintent.getbroadcast (                    alarmcontroller.this, 0, intent, 0);            and cancel the alarm.            Alarmmanager am = (alarmmanager) getsystemservice (alarm_service);            Am.cancel (sender);

Alarmmanager description

Alarmmanager This class provides access to the system alarm service.

The access to it is through the system service:

Context.getsystemservice (Context.alarm_service).

Related method Description:

The Cancel (Pendingintent operation) method will cancel any alarms that match intent.

For a match on Intent, see the description of the Filterequals (Intent Other) method, which shows that two Intent are consistent from the point of view of Intent resolution (filtering) (Intent resolution or filtering), The two intent are considered equal. That is to say, the action,data,type,class,categories of intent is the same, the other data are not within the comparison range.

Set (int type, long triggeratmillis, pendingintent operation) method sets an alarm.

Note: For timing operations, it is possible to use handler more efficiently and simply.

  Note When setting the alarm:

1. If the declared Triggeratmillis is a past time, the alarm will be triggered immediately.

2. If an alarm with the same intent has been set, the previous alarm will be canceled and replaced by the newly set alarm.

Note that the same Intent refers to the Intent in the definition of filterequals (Intent).

The alarm clock is a broadcast, the receiver needs to define and register itself, register using dynamic registration (Registerreceiver (Broadcastreceiver, intentfilter)) or static registration (<receiver> tag in an Androidmanifest.xml file) is available.

setrepeating (int type, long triggeratmillis, long intervalmillis, pendingintent operation) method will set a repeating alarm.

One more interval parameter than the set method.

Type is four kinds:

Elapsed_realtime, Elapsed_realtime_wakeup, RTC, Rtc_wakeup.

The difference is the time standard and whether the device is awakened in sleep state.

Check out the official documentation for details no longer explained.

Instance

For example, to set a recurring alarm that wakes up every 21:30:

    private static final int INTERVAL = $ * * 24;//24h//...        Intent Intent = new Intent (context, requestalarmreceiver.class);        Pendingintent Sender = Pendingintent.getbroadcast (context,                request_code, intent, Pendingintent.flag_cancel_ current);        Schedule the alarm!        Alarmmanager am = (alarmmanager) context                . Getsystemservice (context.alarm_service);        Calendar calendar = Calendar.getinstance ();        Calendar.set (Calendar.hour_of_day, +);        Calendar.set (Calendar.minute,);        Calendar.set (Calendar.second, ten);        Calendar.set (Calendar.millisecond, 0);        Am.setrepeating (Alarmmanager.rtc_wakeup, Calendar.gettimeinmillis (),                INTERVAL, sender);

Use of *android alarm clock 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.