Solution to the problem of invalid startup time setting of Android alarm clock

In Android development, when the AlarmManager is above 5.0, the startup time setting is invalid

To make an app, you need to keep sending heartbeat packets in the background. Since the CPU sleeps after locking the screen, resulting in the heartbeat packet thread being suspended, try to use the AlarmManager to periodically wake up the service to send heartbeat packets.

The following is the code to start the AlarmManager

It turned out to have a strange problem

The incoming time is 2500, that is, every 2.5 seconds. On Hongmi 1s (the system is cm12.1 Android 5.1.1), it takes dozens of seconds to wake up in the bright screen (non sleep) state, and the lock screen (sleep) will not wake up. On Xiaomi 4 (the system is miui7 Android 4.4.4), it is normal in the bright screen state, When the screen is locked, I won't wake up. I tried to rewrite onReceive for broadcastreceiver and onstartcommand for service. They are the same

The reason is that there are restrictions on repeating alarm in Android alarmmanagerservice.

Note: in versions above 19, the frequent setting in setrepeating is only the recommended value, and the minimum value in source codes above 5.0 is 60s

Api19 modification of the above AlarmManager mechanism

Before api19, the AlarmManager provided three methods to set the alarm clock. Because the business needs that the alarm clock only needs one time, set (int type, long starttime, pendingintent PI) is adopted; This method. 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.

In the previous program, the alarm clock settings above api19 are not processed, resulting in no response to the alarm clock settings on mobile phones above 4.4 (if the application is not killed, there is no alarm clock).

For these reasons, setting the alarm clock needs to be processed and set separately according to the API version. The code is as follows:

Jobscheduler above 5.0

For the new jobscheduler ・ API in 5.0, you can read this article first. Here, use jobscheduler above 5.0 to create a scheduled task to regularly detect whether the alarm service exists. If it does not exist, restart the alarm service. (here I set the alarm clock service to detect every minute)

When entering the application, check whether the current system is above 5.0. If so, start the jobscheduler service. The code is as follows:

Builder. Setpersisted (true); The method is whether to restart the task after the device is restarted. It is measured that the task can be restarted.

The above operation further ensures that after the alarm clock service is killed, restart the service. However, doze mode is introduced above 6.0. When mobile phones above 6.0 enter this mode, jobscheduler will stop working.

Processing of doze mode above 6.0

In order to enable jobscheduler to work in doze mode above 6.0, special treatment is made for doze mode above 6.0 - ignoring battery optimization.

1) . add permissions in manifest.xml

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

2) When setting the alarm clock, judge whether the system is above 6.0. If so, judge whether to ignore the optimization of the battery. Determine whether to ignore the battery optimization code as follows:

3) . if battery optimization is not ignored, a reminder dialog box will pop up to prompt the user to ignore battery optimization. The code is as follows:

Override the onactivityresult method in the interface to capture the user's selection. For example, the code is as follows:

supplement

When the application is killed, but the alarm clock service is not killed, the alarm clock is set again. This means that the set alarm clock is not placed in the alarm clock service. Therefore, in this case, the set alarm clock will fail. In order to solve this situation, use Aidl (interprocess communication required by alarm service in another process) to call the reset alarm method of alarm service to reset the alarm clock.

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's an explanation. When the service is started and bound, unbindservice will not stop the service. See this article for details. here

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
分享
二维码
< <上一篇
下一篇>>