Android - Alarm manager runs every day

前端 未结 2 1162
無奈伤痛
無奈伤痛 2021-01-25 01:21

I don\'t understand what\'s wrong but my alarm runs every single day even if I hardcode the day number, I have no idea whats going on...

Intent notificationInten         


        
相关标签:
2条回答
  • 2021-01-25 01:26

    Why is this line necessary?

    calendar.setTimeInMillis(System.currentTimeMillis());
    

    Also, the android development documentation recommends using

    setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation);
    

    EDIT: I suspect the intent isn't set for creating alarms so it's not creating the alarms correctly. Maybe this would work:

    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    //set repeating alarm here
    

    I hope that answers the question.

    0 讨论(0)
  • 2021-01-25 01:50

    So okay i have solved this , for everyone who want to create an alarm application like this > you set the time and hour and the days of week to trigger the alarm, it works like this : if today is the day which you choosed you just set the alarm with this :

    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    

    but IF the day you picked is not the day which is today you have to the the date of the nearest day you picked and then just set the additional hour and minute as above.

    this is the only way to make it work

    0 讨论(0)
提交回复
热议问题