我已经按照创建第一个闹钟提醒的方式完成了这个操作。
private void startAlarm(Calendar calendar) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Config.NOTIFICATION_REQUEST_CODE, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
}
所以,为了暂停通知或闹钟,我创建了另一个闹钟,但那不是重复的闹钟。它只会触发一次。
private void snoozeAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, Config.NOTIFICATION_REQUEST_CODE, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
Calendar.getInstance().getTimeInMillis() + 5 * 60000,
pendingIntent);
finish();
System.exit(0);
}
如果有人需要查看 AlertReceiver.class
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent = new Intent();
intent.setClass(context, DisplayedOnScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder notificationBuilder = notificationHelper.getNotification();
notificationHelper.getManager().notify(1, notificationBuilder.build());
}
}