我的闹钟管理器工作正常,但每次我在闹钟时间之后打开我的应用程序都会触发通知。怎么说警报管理器它只做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);
}
ALARM RECEIVER 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());
}
}
答案 0 :(得分:0)
根据文档,这个问题可以在确切时间和下一次迭代之间的任何时间发生。因此,如果您每天设置2PM,警报可能会在下午2点到次日2点之间熄灭。
因此,如果时间是在闹钟之前或之后,请添加额外检查
var offset = TimeZoneInfo
.FindTimeZoneById(model.TimeZone)
.GetUtcOffset(model.Date)
var dateTimeOffset = new DateTimeOffset(model.Date.Add(model.Time), offset);