Android is a relatively perfect solution for setting alarm clock

A few days ago, someone asked me if I had set up the alarm clock demo in the background of my "non famous programmer" WeChat official account. I said I promised to write one for you. I haven't had any time. Until recently someone asked me to do it. I decided to take the time to write one. Indeed, setting the alarm clock is a more troublesome thing. The demo I wrote here is extracted and encapsulated with a class library. You can directly call the methods of setting and canceling the alarm clock. You can set the daily alarm clock, multi-choice alarm clock from Monday to Sunday, or one-time alarm clock, which is basically the same as the way the system sets the alarm clock.

design sketch

Before the analysis and explanation, let's take a look at the effect. The effect diagram is as follows:

Setting interface

Alarm clock reminder interface

AlarmManager

I won't introduce the methods in AlarmManager one by one. If they are all introduced, it will be dark. The AlarmManager class provides an access interface to the system alarm clock service.

Before API 19, there were three common methods for AlarmManager:

* set(int type,long startTime,PendingIntent pi); This method is used to set a one-time alarm clock. The first parameter represents the alarm clock type, the second parameter represents the alarm clock execution time, and the third parameter represents the alarm clock response action.

* setRepeating(int type,long startTime,long intervalTime,PendingIntent pi); This method is used to set the repeated alarm clock. The first parameter represents the alarm clock type, the second parameter represents the first execution time of the alarm clock, the third parameter represents the interval between two executions of the alarm clock, and the third parameter represents the alarm clock response action.

* setInexactRepeating(int type,long startTime,long intervalTime,PendingIntent pi); This method is also used to set the repeated alarm clock, which is similar to the second method, but the interval between the execution of the two alarm clocks is not fixed.

Starting from API 19, the mechanism of AlarmManager is inaccurate transmission. The operating system will convert the alarm clock to minimize wake-up and battery use. Targetsdkversion will continue to use the previous behavior before API 19. All alarm clocks will be delivered accurately when accurate delivery is required.

Since API 19, the following methods have been adopted: * SetWindow (int, long, pendingintent) * setexact (int, pendingintent) from the above two methods, we can see that without repeat, the alarm clock can only ring once, and both methods can be set accurately. The first method should save power compared with the second method. Because SetWindow this method allows applications to use batteries to optimize alerts from delivery batches, even if it requires moderate timeliness.

Main problems

1. Since API 19, there has been no repeated setting. What if you set an alarm clock to remind you accurately every day? 2. Is the alarm clock still valid after the phone is restarted? 3. Is the alarm clock still valid after the application is killed?

To tell you the truth, I'm sure you've all encountered these problems, and it's really hard to solve them. Let's see how we can solve them one by one.

Solve the pit encountered

How to set a repeat alarm clock after API 19

We know that after we set the reminder using AlarmManager, we receive it through broadcast. As soon as the set reminder time comes, the system sends our customized broadcast, and we receive the application reminder. When reminding, we can reset it again, which solves the problem of setting repeated alarm clock in API 19.

According to the system version, use different methods to set the alarm clock. Next, we receive the notification from the broadcast system to remind the alarm clock.

Through the above broadcast, we can see that I judge whether the alarm clock is repeated after API 19 by whether the time interval is zero. If it is not 0, I will reset it again. Let's take a look at the setalarmtime () method. As follows:

In this way, the problem of repetition is solved.

How to solve the alarm clock failure after the mobile phone is restarted

Yes, after the mobile phone restarts, the alarm clock does fail. To solve this problem, set another broadcast to monitor the mobile phone restart. When the mobile phone restarts, reset it again to solve the above problem.

Look, I called my encapsulated method to reset the alarm clock in the mobile phone restart broadcast. This solves the problem that the alarm clock fails after the mobile phone is restarted. Note: the broadcast needs to be registered in the list file. Don't forget, little friends. I won't post the code here.

The application was killed and the alarm clock failed

What I can tell you is that there is really no good solution for this. If you write a service in your program, you can judge to re register the alarm clock when the service is restarted, or re register it when you open the application. Anyway, it is a place conducive to alarm clock registration and reset it. If the alarm set ID is the same, the alarm set later will automatically overwrite the previously set alarm. If anyone has a better solution to the problem of alarm clock failure after the application is killed, you are welcome to share it.

The benefits of this encapsulated class library

The advantage is that I have encapsulated all the methods for you and can call them directly.

*Just pass in the time division value directly. For example, directly transfer in a time point: 12:30, and then transfer in whether it is a daily reminder or a week reminder * the alarm clock reminder interface has also been encapsulated in it, which is pretty good. Lazy students don't need to write any more, and dissatisfied students can directly download the class library for modification* I have also encapsulated the method of canceling the alarm clock.

In short, it's very convenient. Just look at the demo directly. Dissatisfied students can directly download the class library for modification.

Demo and class library address: https://github.com/loonggg/Android-AlarmManagerClock

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>