简体   繁体   English

AlarmManager不会按时触发

[英]AlarmManager does not fire on time

I want AlarmManager to be fired at 10:30 AM daily based on user mobile device time. 我希望AlarmManager每天上午10:30根据用户移动设备时间被解雇。 It fires at 10:30 AM for sure but the problem is that after 10:30 AM, It repeats without time like after at every half hour or after any unusual time interval. 它肯定会在上午10:30发射,但问题是在上午10:30之后,它会在没有时间的情况下重复,就像在每半小时之后或在任何不寻常的时间间隔之后。

How to prevent this problem ? 如何防止这个问题? I am calling this on Successfull Login and Registration ButtonCick() Event. 我在成功登录和注册ButtonCick()事件上调用它。 I also want to stop this if user Logout. 如果用户注销,我也想停止此操作。

My code is as below : 我的代码如下:

        Intent myIntent = new Intent(Register.this, AlarmReceiver.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(Register.this,
                0, myIntent, 0);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        Calendar firingCal = Calendar.getInstance();
        Calendar currentCal = Calendar.getInstance();

        firingCal.set(Calendar.HOUR_OF_DAY, 10);
        firingCal.set(Calendar.MINUTE, 30);
        firingCal.set(Calendar.SECOND, 0);

        long intendedTime = firingCal.getTimeInMillis();
        long currentTime = currentCal.getTimeInMillis();

        if (intendedTime >= currentTime) {

            WakeLocker.acquire(getApplicationContext());

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
                    AlarmManager.INTERVAL_DAY, pendingIntent);

            WakeLocker.release();

        } else {

            WakeLocker.acquire(getApplicationContext());

            firingCal.add(Calendar.DAY_OF_MONTH, 1);
            intendedTime = firingCal.getTimeInMillis();

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime,
                    AlarmManager.INTERVAL_DAY, pendingIntent);

            WakeLocker.release();
        }

Code seems fine. 代码似乎很好。 If the target version is 19, 如果目标版本是19,

Note: 注意:

as of API 19, all repeating alarms are inexact. 从API 19开始,所有重复警报都是不精确的。 If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. 如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,每次重新安排如上所述。 Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact. targetSdkVersion早于API 19的旧应用程序将继续将所有警报(包括重复警报)视为精确警报。

Source: http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent) 资料来源: http //developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent)

Note: 注意:

Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. 从API 19开始,传递给此方法的触发时间被视为不精确:警报将不会在此时间之前传递,但可能会延迟并在稍后传递。 The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. 操作系统将使用此策略在整个系统中将警报“批处理”在一起,从而最大限度地减少设备需要“唤醒”并最大限度地减少电池使用的次数。 In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future. 一般而言,只要在将来安排的警报,就不会延迟在不久的将来安排的警报。

With the new batching policy, delivery ordering guarantees are not as strong as they were previously. 采用新的配料政策,交货订单保证不像以前那么强大。 If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. 如果应用程序设置了多个警报,则这些警报的实际交付顺序可能与其请求的交付时间的顺序不匹配。 If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; 如果您的应用程序具有强大的订购要求,则可以使用其他API来获取必要的行为; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). 请参阅setWindow(int,long,long,PendingIntent)和setExact(int,long,PendingIntent)。

Applications whose targetSdkVersion is before API 19 will continue to get the previous alarm behavior: all of their scheduled alarms will be treated as exact. targetSdkVersion在API 19之前的应用程序将继续获得先前的警报行为:所有计划的警报将被视为准确。

Source: http://developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent) 资料来源: http //developer.android.com/reference/android/app/AlarmManager.html#set(int,long,android.app.PendingIntent)

Please check if that works for your requirement. 请检查这是否适合您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM