使用 Android WorkManager 代替 AlarmManager 进行本地通知
Using Android WorkManager for local Notification instead of AlarmManager
用户选择一个时间来通知消息作为提醒,可以每天重复或仅在 week.Previously 的特定日期重复,我通过将时间设置为 AlarmManager
来完成此任务
Calendar calSet= Calendar.getInstance();
//calSet.set(Calendar.DAY_OF_WEEK, dayOfWeek); for a particular day of the week
calSet.set(Calendar.HOUR_OF_DAY, hrs));
calSet.set(Calendar.MINUTE, min));
calSet.set(Calendar.SECOND, 00);
calSet.set(Calendar.MILLISECOND, 00);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
现在,AlarmManager 在设备处于 Doze 时停止工作。我无法使用 setAndAllowWhileIdle,因为我需要重复通知消息。
现在我有一个选项可以使用WorkerManager,但我不知道如何设置 AlarmManager
中的时间
PeriodicWorkRequest.Builder(SetJobWork.class,1,TimeUnit.DAYS)
.addTag(Id).setConstraints(constraints).build()
假设我需要运行每天下午1点的任务,出路是什么。
Now I have one option to use WorkerManager,but I have no idea of how to set the time as done in AlarmManager
WorkManager
不支持您的场景,抱歉。
I cannot use setAndAllowWhileIdle because I need to repeat the notification message.
您可以通过调用 setAndAllowWhileIdle()
手动重复安排下一个闹钟,作为处理 PendingIntent
上一个闹钟的一部分。
用户选择一个时间来通知消息作为提醒,可以每天重复或仅在 week.Previously 的特定日期重复,我通过将时间设置为 AlarmManager
来完成此任务 Calendar calSet= Calendar.getInstance();
//calSet.set(Calendar.DAY_OF_WEEK, dayOfWeek); for a particular day of the week
calSet.set(Calendar.HOUR_OF_DAY, hrs));
calSet.set(Calendar.MINUTE, min));
calSet.set(Calendar.SECOND, 00);
calSet.set(Calendar.MILLISECOND, 00);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
现在,AlarmManager 在设备处于 Doze 时停止工作。我无法使用 setAndAllowWhileIdle,因为我需要重复通知消息。
现在我有一个选项可以使用WorkerManager,但我不知道如何设置 AlarmManager
PeriodicWorkRequest.Builder(SetJobWork.class,1,TimeUnit.DAYS)
.addTag(Id).setConstraints(constraints).build()
假设我需要运行每天下午1点的任务,出路是什么。
Now I have one option to use WorkerManager,but I have no idea of how to set the time as done in AlarmManager
WorkManager
不支持您的场景,抱歉。
I cannot use setAndAllowWhileIdle because I need to repeat the notification message.
您可以通过调用 setAndAllowWhileIdle()
手动重复安排下一个闹钟,作为处理 PendingIntent
上一个闹钟的一部分。