In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Alarmmanager to achieve a timing alarm clock in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Alarmmanager to achieve a timing alarm clock in Android".
Alarmmanager mainly manages the hardware clock. Some time-related applications, such as calendars and alarm clocks, need to use AlarmManager services. The function of Alarmmanager is relatively simple, and the related code is located in:
Frameworks/base/core/jni/server/com_android_server_AlarmManagerService.cppframeworks/base/services/java/com/android/server/AlarmManagerService.java
I. frameworks/base/core/jni/server/com_android_server_AlarmManagerService.cpp
This part of the code directly manages the hardware clock and the device name is / dev/alarm. This includes turning on the device, turning off the device, setting the time zone, setting the trigger time (timeout), and waiting for the clock to trigger.
II. Frameworks/base/services/java/com/android/server/AlarmManagerService.java
This part encapsulates the code in directory 1, provides the java interface up, interacts with the client (such as calendar), receives the clock setting request from the client, and notifies the client when the clock is triggered.
Alarm is an independent method of triggering Intent at a predetermined time.
Alarm are beyond the scope of the application, so they can be used to trigger an application event or action, even after the application shuts down. Combined with BroadcastReceiver, they can become particularly powerful, starting applications or performing actions by setting Alarm, while applications do not need to be open or active.
For example, you can use Alarm to implement an alarm clock program, perform normal web queries, or schedule time-consuming or costly operations during "off-peak" hours.
For timing operations that occur only during the application life cycle, the combination of the Handler class with the Timer and Thread classes is a better choice, allowing Android to have more control over system resources.
The Alarm in Android remains active while the device is in sleep mode and can be set to wake up the device; however, all Alarm are canceled when the device is rebooted.
The operation of Alarm is handled by AlarmManager, and its system services can be obtained through getSystemService, as shown below:
AlarmManageralarms= (AlarmManager) getSystemService (Context.ALARM_SERVICE)
To create a new Alarm, use the set method and specify an Alarm type, trigger time, and Intent to call when the Alarm is triggered. If the Alarm you set occurs in the past, it will be triggered immediately.
There are four types of Alarm. Your choice will determine what the time value you pass in the set method represents, a specific time or the passage of time:
RTC_WAKEUP
At the specified time (when Alarm is set), wake up the device to trigger Intent.
RTC
Intent is triggered at an explicit time, but the device is not awakened.
ELAPSED_REALTIME
After booting the device, if the elapsed time reaches the total time, then Intent is triggered, but the device is not awakened. The elapsed time includes any time the device sleeps. It is important to note that the time elapse is calculated from the time it is started.
ELAPSED_REALTIME_WAKEUP
After booting the device and reaching the total elapsed time, the device will be awakened and Intent will be triggered if necessary.
The creation process of Alarm is shown in the following snippet:
IntalarmType=AlarmManager.ELAPSED_REALTIME_WAKEUP; longtimeOrLengthofWait=10000; StringALARM_ACTION= "ALARM_ACTION"; IntentintentToFire=newIntent (ALARM_ACTION); PendingIntentpendingIntent=PendingIntent.getBroadcast (this,0,intentToFire, 0); alarms.set (alarmType,timeOrLengthofWait,pendingIntent)
When Alarm arrives, the PendingIntent you specified will be triggered. Set up another Alarm and replace the previously existing Alarm with the same PendingIntent.
Cancel an Alarm, call the cancel method of AlarmManager, and pass in the PendingIntent that you no longer want to be triggered, as shown in the following code:
Alarms.cancel (pendingIntent)
In the following code snippet, two Alarm are set, and then * Alarm is canceled immediately. * Alarm explicitly sets the wake-up device and sends Intent at a specific time. The second setting is that after the slave device is started, the elapsed time is 30 minutes, and if the device is asleep, it will not wake up after the arrival time.
AlarmManageralarms= (AlarmManager) getSystemService (Context.ALARM_SERVICE)
StringMY_RTC_ALARM= "MY_RTC_ALARM"
StringALARM_ACTION= "MY_ELAPSED_ALARM"
PendingIntentrtcIntent=PendingIntent.getBroadcast (this,0,new Intent (MY_RTC_ALARM), 1)
PendingIntentelapsedIntent=PendingIntent.getBroadcast (this,0,new Intent (ALARM_ACTION), 1)
/ / Wakeupandfireintentin5hours. (there may be errors in the comments)
Datet=newDate ()
T.setTime (java.lang.System.currentTimeMillis () + 60 / 1000 / 5)
Alarms.set (AlarmManager.RTC_WAKEUP,t.getTime (), rtcIntent)
/ / Fireintentin30minsifalreadyawake.
Alarms.set (AlarmManager.ELAPSED_REALTIME,30*60*1000,elapsedIntent)
/ / Cancelthefirstalarm.
Alarms.cancel (rtcIntent)
Thank you for your reading, the above is the content of "how to use Alarmmanager to achieve a timing alarm clock in Android". After the study of this article, I believe you have a deeper understanding of how to use Alarmmanager to achieve a timing alarm clock in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.