简体   繁体   English

有时不会使用AlarmManager触发警报

[英]Sometimes alarms doesn't get triggered using AlarmManager

I have a reminder app which notifies the user for a specific task. 我有一个提醒应用程序,用于通知用户特定的任务。 Everything is working as expected, but sometimes the alarm doesn't get triggered. 一切都按预期工作,但有时不会触发警报。 This issue was reported by nearly 100 users. 近100个用户报告了此问题。 I suspect that when the devices are in sleep mode, the alarms are not triggered. 我怀疑当设备处于睡眠模式时,不会触发警报。 I am facing this issue for nearly 1 year and I haven't found any solution yet. 我面临这个问题将近一年,但尚未找到任何解决方案。

This is how the alarms are getting scheduled: 这是警报的调度方式:

public void schedule(Context context, long nextTrigger, Intent intent) {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQ_CODE_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, nextTrigger, pendingIntent);
    }
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        alarmManager.setExact(ALARM_TYPE, nextTrigger, pendingIntent);
    }
    else {
        alarmManager.set(ALARM_TYPE, nextTrigger, pendingIntent);
    }
}

This is the broadcast receiver from pending intent: 这是来自未决意图的广播接收器:

public class ReminderAlarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = null;
        if (pm != null) {
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, APP_NAME);
            if (wl != null)
                wl.acquire(10000L);
        }

        Log.log("Reminder alarm received");

        ReminderJobIntentService.enqueueWork(context, ReminderJobIntentService.class, JOB_ID_REMINDER_ALARM, intent);

        if (wl != null)
            wl.release();
    }
}

This is the Job Intent Service which is enqueued from the broadcast: 这是从广播入队的求职服务:

public class ReminderJobIntentService extends JobIntentService {

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.log("Reminder job intent service");

        // Creates a notification on the UI
        Utils.createNotification();

        // Schedule the next task (alarm). Actually it's schedule(context, long, intent) but it calculates the next trigger. That's it
        Scheduler.getInstance().scheduleNext();
    }
}
  1. I have logs that the alarms were scheduled correctly. 我有警报已正确安排的日志。 When the broadcast is received (when the alarm is triggered) I'm logging this as well. 当接收到广播时(触发警报时),我也将其记录下来。 When there are no logs with the broadcast, it means that the alarm doesn't triggered. 如果广播中没有日志,则表示不会触发警报。
  2. The alarms are scheduled at Boot Complete 警报计划在Boot Complete
  3. I have tried to reproduce this issue several times with different Android devices and versions but I can't reproduce it. 我尝试使用不同的Android设备和版本重现此问题几次,但无法重现。 I even tried to put the device in Doze Mode and Battery Unpluged , but no luck. 我什至尝试将设备置于Doze ModeBattery Unpluged ,但是没有运气。 The alarms were triggered. 警报被触发。 I left the device untouched for nearly 3 days, the alarms were triggered with a 4 minute delay (because of Android OS is batching the alarms). 我将设备保持了将近三天的时间,但由于延迟了4分钟而触发了警报(因为Android OS正在批量处理警报)。
  4. When a user is complaining about the reminders are not received, usually I redirect them to "Settings -> Battery -> Ignore battery optimization for app". 当用户抱怨没有收到提醒时,通常我会将其重定向到“设置->电池->忽略应用程序的电池优化”。 But sometimes this doesn't fix the issue. 但这有时不能解决问题。

What have I noticed that the people who are complaining about this are using Samsung devices: S6, S7, S8 models with Android 6, 7. (Maybe because 45% of my users are using this type of device?) 我注意到哪些人在抱怨使用三星设备:配备Android 6、7的S6,S7,S8机型(也许是因为我有45%的用户正在使用这种设备?)

Have you ever encountered this specific problem? 您是否遇到过此特定问题? Am I missing something? 我想念什么吗? Are Samsung models killing the apps in the background? 三星机型是否在后台杀死了这些应用程序? Maybe some 3rd party apps are killing the apps? 也许某些第三方应用正在杀死这些应用?

I have encountered a similar problem while building an app to get daily notifications. 我在构建用于获取每日通知的应用程序时遇到了类似的问题。 The problem is not with the code, it's with the way Alarms are handled in Android Kitkat and above. 问题不在于代码,而在于Android Kitkat及更高版本中处理警报的方式。 Alarms are now optimized to minimize wakeups and battery use. 现在优化了警报,以最大程度地减少唤醒和电池消耗。 So they may fire at different times or not fire at all. 因此它们可能在不同时间开火或根本不开火。 You can look here for more info: 您可以在这里查看更多信息:

AlarmManager not working properly AlarmManager无法正常工作

Do you know why nigth alarm applications usually work well? 您知道为什么黑夜警报应用程序通常能正常工作吗? ... because the phone is usually connected to the charger and then it does not go into sleep mode ...因为手机通常已连接至充电器,因此无法进入睡眠模式

My proposal: Start a foreground Service, that's a Service than calls startForeground() and show a non dismissable notification, you have to acquire a partial wakelock too. 我的建议:启动前台服务,该服务比调用startForeground()并显示不可撤销的通知,还必须获取部分唤醒锁。

Doing that the device will not enter deep sleep and the Alarms should be triggered correctly ... but at a cost of draining battery 这样一来,设备将不会进入深度睡眠状态,并且应该正确触发警报...但是会消耗电池电量

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

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