这就是我创建闹钟的方式
PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
startTime.set(Calendar.MINUTE, minute);
startTime.set(Calendar.SECOND, 0);
if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
startTime.add(Calendar.DAY_OF_MONTH, 1);
}
long intendedTime = startTime.getTimeInMillis();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, alarmIntent);
我在 BroadcastReceiver 中调用的方法:
private void startAlarm(Context context) {
Intent mainIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager myNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.drawable.l_active)
.setContentTitle(context.getString(R.string.morn_title))
.setContentText(context.getString(R.string.morn_text))
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent);
myNotificationManager.notify(1, builder.build());
}
所以我遇到了像这里这样的问题。当我将闹钟设置为 9:51 而我的时间是 10:00 时 - 闹钟不起作用。添加后
if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
startTime.add(Calendar.DAY_OF_MONTH, 1);
}
警报工作正常,但是当我重新启动手机时 - 它不起作用。我会试着用一个例子来解释:我手机上的时间是23:50。我在 00:05 设置闹钟,然后我重新启动手机并等到 00:05。而且闹钟不响。但!如果我不重启手机 - 一切正常。还有一件事:当我没有设置过去的时间时,我的手机重启没有问题(例如,我的时间是 22:00,我将时间设置为 22:05,重启手机就可以了)
万千封印
相关分类