alarm and notification not triggered at specified time

后端 未结 2 1755
有刺的猬
有刺的猬 2021-01-17 03:31

I am trying to trigger a notification and an alarm at a specified time. I have put log information to console to see if the correct time is being set and it is fine. However

相关标签:
2条回答
  • 2021-01-17 03:39

    Take a look at the documentation of the setRepeating method:

    Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

    Your alarms will be inexact always using setRepeating, try using setExact and reprograming the alarm when it's triggered.

    Also, try extending WakefulBroadcastReceiver instead of BroadcastReceiver in NotificationMessage in order to be able to wake the service when your app isn't running.

    0 讨论(0)
  • 2021-01-17 03:43

    Inside btn_add_task listener, set the alarm as below -

    Intent notifyMessage = new Intent(getApplicationContext(),NotificationMessage.class);
    PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, notifyMessage, PendingIntent.FLAG_UPDATE_CURRENT);    
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pi);
    

    Now, put a log inside onReceive of NotificationMessage class to be sure that the alarm is fired on time. Hope, now your program will run.

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