Android Alarm Clock Mechanism Realizes Timing Task Function

  • 2021-08-12 03:42:22
  • OfStack

The alarm clock implementation mechanism of Android needs to call AlarmManager. set () to record the alarm time to the system. When the alarm time arrives, the system will send a broadcast to the application. We only need to register the broadcast receiver.

This article is divided into three parts to explain how to realize the alarm clock:

Directory:

1. Set the alarm time;
2. Receiving alarm event broadcast;
3. Recalculate and set the alarm time after restarting;

1. Set the alarm time (milliseconds)


private void setAlarmTime(Context context, long triggerAtMillis) { 
  AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
  Intent intent = new Intent("android.alarm.demo.action"); 
  PendingIntent sender = PendingIntent.getBroadcast( 
    context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
  // Alarm interval,   Here set to 1 Minute disturbance 1 The second time, in the first 2 Step by step, we will every other step 1 Received in minutes 1 Subbroadcast  
  //int interval = 60 * 1000; 
  //am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender); 
  am.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, sender); 
 } 

The second parameter is roughly divided into two types: one is relative time and the other is absolute time.

Therefore, the triggerAtTime settings vary depending on the type used.

If you use ELAPSED_REALTIME_WAKEUP, you should call SystemClock. elapsedRealtime () to get the relative time plus the delay time you set.

2. Receive alarm event broadcasts


public class AlarmReceiver extends BroadcastReceiver { 
 public void onReceive(Context context, Intent intent) { 
  if ("android.alarm.demo.action".equals(intent.getAction())) { 
   //  No. 1 1 When the alarm time set in step arrives, an alarm prompt can be popped up here and the bell can be played  
   Toast.makeText(context, "hello alarm", Toast.LENGTH_LONG).show(); 
   System.out.println("hello alarm"); 
   //  You can continue to set the following 1 Secondary alarm time ; 
   return; 
  } 
 } 
} 

Of course, Receiver needs to be registered in Manifest. xml:


<receiver android:name="AlarmReceiver"> 
   <intent-filter> 
    <action android:name="android.alarm.demo.action" /> 
   </intent-filter> 
  </receiver> 

3. Recalculate and set alarm time after reboot

Of course, there must be an BootReceiver:


public class BootReceiver extends BroadcastReceiver { 
 public void onReceive(Context context, Intent intent) { 
  String action = intent.getAction(); 
  if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 
   // Recalculate the alarm time and adjust the number 1 Setting alarm time and alarm interval time by step  
  } 
 } 
} 

Of course, you also need to register:


<receiver android:name="BootReceiver"> 
 <intent-filter> 
  <action android:name="android.intent.action.BOOT_COMPLETED" /> 
 </intent-filter> 
 </receiver> 

I had one problem setting the clock
I started the code as follows


alarmManager.set(AlarmManager.RTC_WAKEUP, (5*1000), sender); 

My original intention was to set the alarm clock to start after 5 seconds, but every time I set the alarm clock, it started immediately.
Later, I found out that the problem was that I misunderstood him in the second parameter

I thought it was a "delay" time, but in fact it was a "start" time.
To understand this parameter, look at the parameter type


public static final int ELAPSED_REALTIME 
 // When the system goes to sleep, this type of alarm will not wake up the system. It is not transmitted until the next time the system is awakened. The time taken by the alarm is relative time and is timed after the system is started , Including sleep time, you can call the SystemClock.elapsedRealtime() Get. The system value is 3 (0x00000003) .  

public static final int ELAPSED_REALTIME_WAKEUP 
 // Able to wake up the system. Usage is the same as ELAPSED_REALTIME The system value is 2 (0x00000002)  .  

public static final int RTC 
 // When the system goes to sleep, this type of alarm will not wake up the system. It is not transmitted until the system is awakened next time. The time taken by this alarm is absolute time, and the time taken is UTC Time, you can call the  System.currentTimeMillis() Get. The system value is 1 (0x00000001)  .  
 
public static final int RTC_WAKEUP 
 // Able to wake up the system. Usage is the same as RTC Type, the system value is  0 (0x00000000)  .  

It can be roughly divided into two types: one is relative time and the other is absolute time.

Therefore, the triggerAtTime settings vary depending on the type used.

If you use ELAPSED_REALTIME_WAKEUP type, you should call SystemClock. elapsedRealtime () to get the relative time plus the delay time you set.


alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+5000, sender); 

If you use RTC_WAKEUP type, you should call System. currentTimeMillis () to get the time since 1970.1.1 plus the delay time you set


alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender); 

The setRepeating method has four parameters, which have the following meanings:

type: Indicates the alert type, and the values available for 1 are AlarmManager.RTC and AlarmManager.RTC_WAKEUP. If the type parameter value is set to AlarmManager. RTC, it means that it is a normal timer. If the type parameter value is set to AlarmManager.RTC_WAKEUP, in addition to the timer function, it will also sound an alarm (for example, bell, vibration).

triggerAtTime: The time to wait for the first run, that is, the execution delay time, in milliseconds.

interval: Indicates the time interval for execution, in milliseconds.

operation: An PendingIntent object that represents the action to be performed after the time has elapsed. PendingIntent is similar to Intent and can encapsulate Activity, BroadcastReceiver, and Service. But unlike Intent, PendingIntent can exist outside of the application.


Related articles: