Setting an alarm on Android is not as simple as setting an alarm on iOS. Developers who have done this know how difficult it is. Here is my solution to setting an alarm on Android.
Main Issues
Modification of AlarmManager mechanism above API19 Before API 19, AlarmManager provided three methods to set alarms. Since the alarm only needs to be set once, the method set(int type, long startTime, PendingIntent pi) is used. Starting from API 19, the mechanism of AlarmManager is inaccurate transmission. The operating system will convert the alarm to minimize wake-up and battery usage. Because the previous program did not handle the alarm settings above API19, setting the alarm on phones above 4.4 was unresponsive (there was no alarm even if the application was not killed). Therefore, setting the alarm needs to be processed and set according to the API version. The code is as follows:
This ensures that the alarm is set even when the application is not killed. What to do when an application is killed After the application is killed, the set alarm becomes invalid. Here, the daemon process and gray keep alive are used to ensure that the background alarm service is not killed. When the application and the alarm service are killed, the daemon process and gray keep alive restart the alarm service and reset the alarm. Regarding the processing of daemon processes, an open source daemon library is used here. Android-AppDaemon Add the open source daemon Android-AppDaemon to the onCreate of the alarm service. The code is as follows:
To further ensure the survival of the alarm service, add gray keep-alive (using the system vulnerability to start the foreground Service). The code is as follows:
The above operation can improve the survival of the alarm service as much as possible. However, on mobile phones with version 5.0 or higher, the alarm service will be completely killed when the system's built-in Clean function is used. In order to solve the problem above version 5.0, the new feature JobScheduler of version 5.0 or higher is introduced here. JobScheduler 5.0 and above You can read this article first about the new JobScheduler API in 5.0. Here, we use JobScheduler 5.0 or higher to create a scheduled task, which periodically checks whether the alarm service exists. If it does not exist, we restart the alarm service. (Here, I set the alarm service to be checked once every minute) When entering the application, check whether the current system is 5.0 or above. If it is, start the JobScheduler service. The code is as follows:
The builder.setPersisted(true); method is used to check whether to re-execute the task after the device is restarted. It has been tested that the task can be restarted. The above operation further ensures that the alarm service is restarted after being killed. However, Doze mode is introduced in 6.0 and above. When a mobile phone above 6.0 enters this mode, JobScheduler will stop working. Doze mode processing above 6.0 In order to allow JobScheduler to work in Doze mode in versions above 6.0, special processing is done here for Doze mode above 6.0 - ignoring battery optimization.
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
If the battery optimization is not ignored, a reminder dialog box will pop up to prompt the user to ignore the battery optimization operation. The code is as follows:
Rewrite the onActivityResult method in the interface to capture the user's selection. For example, the code is as follows:
Replenish When the application is killed, but the alarm service is not killed, the alarm is set again. This means that the set alarm is not put into the alarm service. So in this case, the set alarm will be invalid. To solve this problem, use AIDL (the alarm service is in another process and needs inter-process communication) to call the reset alarm method of the alarm service to reset the alarm. Start the alarm service in the onCreat() method of the application, and then bind the alarm service.
In the onDestroy() method, call the reset alarm method of the alarm service. The code is as follows:
Here is a note, when the service is started and bound, unbindService will not stop the service. For details, please refer to this article. *** The above does not mean that the alarm clocks of all Android phones can be used. This just tries to ensure that most phones can be used. |
<<: ASUS's unique aesthetics of technology and art
>>: iTunes cannot verify the server? Try this solution
It has been rumored that Apple's iPhone 6 wil...
What is a private domain operator ? As the name s...
We often see words like "zero trans fatty ac...
Have you ever noticed those ocean-going cargo shi...
There is only one core strategy for the growth of...
I finally waited for you, and luckily I didn’t gi...
Tencent's motivation for making TV Sun Peng, ...
Nowadays, many offline businesses are trying out ...
Before starting the article, I would like to shar...
At the beginning of the year, Apple released the ...
The screen size of ASUS ZenFone 6 is 6.0 inches, w...
Just as there are a thousand Hamlets in the eyes ...
Whether it is Facebook advertising or any other t...
What is the price for customizing Bijie catering ...
In this article, the author will analyze with you...