警报管理器也在日期之后发出通知
Alarm manager make notification also after the date
我的闹钟管理器工作正常,但每次我在闹钟时间后打开我的应用程序时,它都会触发通知。我怎么能说警报管理器它只发出 1 个通知,并且在警报时间后它应该停止。
public void startTimer(int year,int month, int day,int hour,int minute) {
int time_year;
int time_month;
int time_day;
int time_hour;
int time_minute;
time_minute = minute;
time_hour = hour;
time_month = month-1; // BC 0 januar and 11 dezember
time_day = day;
time_year = year;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, time_year);
calendar.set(Calendar.MONTH, time_month);
calendar.set(Calendar.DAY_OF_MONTH, time_day);
calendar.set(Calendar.HOUR_OF_DAY, time_hour);
calendar.set(Calendar.MINUTE, time_minute);
calendar.set(Calendar.SECOND, 1);
Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
报警接收器CLASS
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("myApp", "ALARM RECEIVER RECEIVED");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//if we want ring on notifcation then uncomment below line//
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).
setSmallIcon(R.drawable.online_icon).
setContentIntent(pendingIntent).
setContentText("Ein Event startet bald").
setContentTitle("Hast du heute schon was vor?").
// setSound(alarmSound).
setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
根据文档,此问题可能会在确切时间和下一次迭代之间的任何时间发生。因此,如果您每天设置下午 2 点,闹钟可能会在下午 2 点到第二天下午 2 点之间响起。
所以添加额外的检查时间是在闹钟之前还是之后
long timeToAlarm = calendar.getTimeInMillis();
if (calendar.getTimeInMillis() < System.currentTimeMillis()){
timeToAlarm += (24*60*60*1000);
};
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
我的闹钟管理器工作正常,但每次我在闹钟时间后打开我的应用程序时,它都会触发通知。我怎么能说警报管理器它只发出 1 个通知,并且在警报时间后它应该停止。
public void startTimer(int year,int month, int day,int hour,int minute) {
int time_year;
int time_month;
int time_day;
int time_hour;
int time_minute;
time_minute = minute;
time_hour = hour;
time_month = month-1; // BC 0 januar and 11 dezember
time_day = day;
time_year = year;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, time_year);
calendar.set(Calendar.MONTH, time_month);
calendar.set(Calendar.DAY_OF_MONTH, time_day);
calendar.set(Calendar.HOUR_OF_DAY, time_hour);
calendar.set(Calendar.MINUTE, time_minute);
calendar.set(Calendar.SECOND, 1);
Intent intent = new Intent(getActivity(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
报警接收器CLASS
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("myApp", "ALARM RECEIVER RECEIVED");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context,MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//if we want ring on notifcation then uncomment below line//
// Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context).
setSmallIcon(R.drawable.online_icon).
setContentIntent(pendingIntent).
setContentText("Ein Event startet bald").
setContentTitle("Hast du heute schon was vor?").
// setSound(alarmSound).
setAutoCancel(true);
notificationManager.notify(100,builder.build());
}
}
根据文档,此问题可能会在确切时间和下一次迭代之间的任何时间发生。因此,如果您每天设置下午 2 点,闹钟可能会在下午 2 点到第二天下午 2 点之间响起。
所以添加额外的检查时间是在闹钟之前还是之后
long timeToAlarm = calendar.getTimeInMillis();
if (calendar.getTimeInMillis() < System.currentTimeMillis()){
timeToAlarm += (24*60*60*1000);
};
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);