【发布时间】:2018-01-04 14:44:43
【问题描述】:
我有一个提醒应用程序,它会通知用户特定任务。一切都按预期工作,但有时没有触发警报。近 100 位用户报告了此问题。我怀疑当设备处于睡眠模式时,不会触发警报。我面临这个问题将近 1 年了,我还没有找到任何解决方案。
这是安排警报的方式:
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);
}
}
这是来自未决意图的广播接收器:
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();
}
}
这是从广播中排队的 Job Intent 服务:
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();
}
}
- 我有日志显示警报安排正确。收到广播时(触发警报时)我也在记录这个。当广播没有日志时,表示没有触发警报。
- 警报安排在
Boot Complete - 我已尝试使用不同的 Android 设备和版本多次重现此问题,但无法重现。我什至尝试将设备放入
Doze Mode和Battery Unpluged,但没有运气。警报被触发。我将近 3 天没有动过设备,警报延迟 4 分钟触发(因为 Android 操作系统正在批处理警报)。 - 当用户抱怨没有收到提醒时,通常我会将他们重定向到“设置 -> 电池 -> 忽略应用程序的电池优化”。但有时这并不能解决问题。
我注意到抱怨此问题的人使用的是三星设备:搭载 Android 6、7 的 S6、S7、S8 型号。(可能是因为我 45% 的用户使用这种类型的设备?)
你有没有遇到过这个特定的问题?我错过了什么吗?三星机型是否会在后台杀死应用程序?也许某些 3rd 方应用正在扼杀这些应用?
【问题讨论】:
-
很想知道您是否找到了解决此问题的方法
标签: android alarmmanager android-alarms