启动时发送的Android通知

启动时发送的Android通知,android,notifications,alarm,Android,Notifications,Alarm,我已经设置了一个闹钟管理器,每天中午都会关机并发送通知,除了一个部分外,它工作得非常好。当我第一次打开应用程序时,无论何时,它都会立即发出通知 Intent myIntent = new Intent(arg0, NotificationService.class); myIntent.putExtra("compDate", tti.getEvents()[0][1]); AlarmManager alarmManager = (AlarmManager)arg0.getSys

我已经设置了一个闹钟管理器,每天中午都会关机并发送通知,除了一个部分外,它工作得非常好。当我第一次打开应用程序时,无论何时,它都会立即发出通知

Intent myIntent = new Intent(arg0, NotificationService.class);
    myIntent.putExtra("compDate", tti.getEvents()[0][1]);
    AlarmManager alarmManager = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(arg0, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR, 12);
    calendar.set(Calendar.AM_PM, Calendar.AM);
    calendar.set(Calendar.DAY_OF_MONTH, 1);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
onCreate方法中的这段代码正在调用该方法

int f = preferences.getInt("numberOfLaunches", 1);

    if(f < 2){
        alarmMethod();
        f++;
        editor.putInt("numberOfLaunches", f);
        editor.apply();
    }
intf=preferences.getInt(“numberoflaunchs”,1);
if(f<2){
报警方法();
f++;
编辑:putInt(“NumberOfLaunchs”,f);
editor.apply();
}

这是唯一一次调用它时,它很可能是由以下原因引起的:

如果规定的触发时间已过,则会立即触发报警


确保您将第一次报警设置为将来的报警。

很可能是由以下原因引起的:

如果规定的触发时间已过,则会立即触发报警


确保您将第一个警报设置为将来。

好的,多亏了这个问题:还有Kuffs的回答,我添加了这段代码,它就像一个符咒

Date dat  = new Date();
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY, 12);
    cal_alarm.set(Calendar.MINUTE, 0);
    cal_alarm.set(Calendar.SECOND, 0);

    if(cal_alarm.before(cal_now)){
        cal_alarm.add(Calendar.DATE,1);
    }

好的,多亏了这个问题:还有Kuffs的回答,我添加了这段代码,它就像一个符咒

Date dat  = new Date();
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY, 12);
    cal_alarm.set(Calendar.MINUTE, 0);
    cal_alarm.set(Calendar.SECOND, 0);

    if(cal_alarm.before(cal_now)){
        cal_alarm.add(Calendar.DATE,1);
    }
现在我设置
calendar.set(calendar.HOUR,14)这是未来的问题,但仍然是相同的问题现在我设置
calendar.set(calendar.HOUR,14)这是在未来,但仍然是相同的问题