Android The implementation mechanism of alarm clock is very simple ,
Just call AlarmManager.set() Record the alarm time in the system , When the alarm time is up , The system will send a broadcast to the application , We just need to sign up for the radio receiver .

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

catalog :
1. Set Alarm Time ;
2. Receive alarm event broadcast ;
3. Recalculate and set alarm time after reboot ;

text :
1. Set Alarm Time ( millisecond )
    private void setAlarmTime(Context context,  long timeInMillis) {
        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);
        int interval = 60 * 1000;// Alarm interval , Set here as 1 One minute , In section 2 Step we will 1 Minutes to receive a broadcast
        am.setRepeating(AlarmManager.RTC_WAKEUP, timeInMillis, interval,
sender)
    }

2. Receive alarm event broadcast
public class AlarmReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (”android.alarm.demo.action“.equals(intent.getAction())) {
            // The first 1 The alarm time set in step is up to , Here you can pop up the alarm and play the alarm
            // You can continue to set the next alarm time ;
            return;
        }
    }
}
of course ,Receiver It needs to be Manifest.xml Registered in :
        <receiver android:name="AlarmReceiver">
            <intent-filter>
                <action android:name="android.alarm.demo.action" />
            </intent-filter>
        </receiver>

3. Recalculate and set alarm time after reboot
One, of course 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 alarm time , And adjust the first step to set the alarm time and alarm interval time
        }
    }
}
of course , Registration is also required :
        <receiver android:name="BootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Technology
©2019-2020 Toolsou All rights reserved,
Python Basic knowledge and notes Programmer Tanabata Valentine's Day confession code NOI2019 travels China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour C Language programming to find a student's grade Software testing BUG describe ESP8266/ESP32 System : Optimize system startup time Unity Scene loading asynchronously ( Implementation of loading interface ) Baidu , Ali , Tencent's internal position level and salary structure , With job suggestions !PYTHON Summary of final review