简体   繁体   English

AlarmManager在错误的时间运行

[英]AlarmManager running at the wrong time

I am trying to put an AlarmManager to run every 3 min but it is running at wrong and varied times, moments in seconds and others does not run. 我试图让AlarmManager每隔3分钟运行一次,但它运行的时间错误且不同,时间以秒为单位,其他时间不运行。 I am trying to test on an Android 7.0 and another 6.0 device and both are running wrong, I saw the following comments but could not fix. 我试图在Android 7.0和另一个6.0设备上测试并且两者都运行错误,我看到以下注释但无法修复。

Alarm Manager Example AlarmManager fires alarms at wrong time Android AlarmManager fire at wrong time 警报管理器示例 AlarmManager在错误的时间触发警报 Android AlarmManager在错误的时间触发

The following code: 以下代码:

long repeatTime = 180000;

        AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intentAlarm = new Intent(context, TimerProcessReceiver.class);
        PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
                intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

        if (android.os.Build.VERSION.SDK_INT < 19) {
            processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
                    repeatTime, pendingIntentAlarm);
        } else {
            processTimer.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() +
                    repeatTime, pendingIntentAlarm);
        }

Still having problems, I've updated as above. 仍然有问题,我已经更新如上。 Update as @Vyacheslav's reply 更新为@ Vyacheslav的回复

long repeatTime = 180000 + System.currentTimeMillis();
    AlarmManager processTimer = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(context, ProcessReceiver.class);
    PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(context, 0,
            intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

    int currentapiVersion = Build.VERSION.SDK_INT;

    if (Build.VERSION.SDK_INT >= 23) {
        processTimer.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                repeatTime, pendingIntentAlarm);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            processTimer.setExact(AlarmManager.RTC_WAKEUP,
                    repeatTime, pendingIntentAlarm);

    } else if (currentapiVersion < Build.VERSION_CODES.KITKAT) {

        processTimer.set(AlarmManager.RTC_WAKEUP,  repeatTime,
                pendingIntentAlarm);

    }

In case I am using two simultaneous timers with the PendingIntent of ids 0 and 1 (but the structure of adding these PendingIntent are the same as the code above) but with the same runtime 3 min. 如果我使用两个同时定时器与ids 0和1的PendingIntent(但添加这些PendingIntent的结构与上面的代码相同),但运行时间相同,为3分钟。 Both are executing the wrong way in a few seconds and randomly. 两者都在几秒钟内随机执行错误的方式。

Use setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) instead. 请改用setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) It helps to wakeup your device. 它有助于唤醒您的设备。

EDIT 编辑

 int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            am.set(AlarmManager.RTC_WAKEUP, nexttime, pi);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                am.setExact(AlarmManager.RTC_WAKEUP, nexttime, pi);
            } else {
                    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nexttime, pi);
            }
        }

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

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