在服务中设置闹钟并在同一服务中接收回调
Set alarm in service and receive callback in the same service
我的 ReminderService 使用 AlarmManager 在特定时间通知用户即将发生的事件。 AlarmManager 是否有可能通知同一个服务(ReminderService),或者我是否需要启动另一个服务来捕捉未决的意图?到目前为止,这个机制看起来像这样
public class ReminderService extends Service {
// ...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// ..
/* When the alarm goes off the NotifyService will be started. Is it possible to inform **this**
service (ReminderService) and to handle the alarm? */
Intent alarmIntent = new Intent(this, NotifyService.class);
alarmIntent.putExtra(TodoTask.PARCELABLE_KEY, task);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
Date date = new Date(reminderTimeStamp*1000);
calendar.setTime(date);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
您可以使用相同的 Service
(或其他组件)来接收警报。但是,这并不是说您的警报不能保证在低电量时按时发送。使用 WakefulReceiver
或您自己的 BroadcastReceiver
和唤醒锁的组合来启动您的 Service
。有关电源状态和警报的更多详细信息,请参阅 this article。
另外请注意,从 Android Marshmallow 开始,如果设备处于打瞌睡模式,您的唤醒锁将被忽略。在打瞌睡时,您还必须执行其他操作才能在特定时间唤醒设备。
是的,你可以。您只需要更改此行:
Intent alarmIntent = new Intent(this, NotifyService.class);
对此:
Intent alarmIntent = new Intent(this, ReminderService.class);
那样 ReminderService
将在同一方法 public int onStartCommand(Intent intent, int flags, int startId)
中接收带有 TodoTask.PARCELABLE_KEY
的意图
请记住,不能保证这将是服务的同一个实例。如果您使用的服务 运行 被系统终止,则会启动一个新的实例来处理意图。
还要记住 Larry_Schiefer 在他的回答中提到的所有睡眠和打瞌睡模式。
我的 ReminderService 使用 AlarmManager 在特定时间通知用户即将发生的事件。 AlarmManager 是否有可能通知同一个服务(ReminderService),或者我是否需要启动另一个服务来捕捉未决的意图?到目前为止,这个机制看起来像这样
public class ReminderService extends Service {
// ...
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// ..
/* When the alarm goes off the NotifyService will be started. Is it possible to inform **this**
service (ReminderService) and to handle the alarm? */
Intent alarmIntent = new Intent(this, NotifyService.class);
alarmIntent.putExtra(TodoTask.PARCELABLE_KEY, task);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0);
Calendar calendar = Calendar.getInstance();
Date date = new Date(reminderTimeStamp*1000);
calendar.setTime(date);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
您可以使用相同的 Service
(或其他组件)来接收警报。但是,这并不是说您的警报不能保证在低电量时按时发送。使用 WakefulReceiver
或您自己的 BroadcastReceiver
和唤醒锁的组合来启动您的 Service
。有关电源状态和警报的更多详细信息,请参阅 this article。
另外请注意,从 Android Marshmallow 开始,如果设备处于打瞌睡模式,您的唤醒锁将被忽略。在打瞌睡时,您还必须执行其他操作才能在特定时间唤醒设备。
是的,你可以。您只需要更改此行:
Intent alarmIntent = new Intent(this, NotifyService.class);
对此:
Intent alarmIntent = new Intent(this, ReminderService.class);
那样 ReminderService
将在同一方法 public int onStartCommand(Intent intent, int flags, int startId)
TodoTask.PARCELABLE_KEY
的意图
请记住,不能保证这将是服务的同一个实例。如果您使用的服务 运行 被系统终止,则会启动一个新的实例来处理意图。
还要记住 Larry_Schiefer 在他的回答中提到的所有睡眠和打瞌睡模式。