警报管理器未按预期触发

时间:2019-03-09 15:23:01

标签: android alarmmanager

我的应用程序中的所有警报都出现问题。我的应用程序的整体思路取决于在特定时间触发警报(如果出现问题,用户将付款(信用))。 这是我写来解释我的问题的警报之一:

@Override
protected void onCreate(Bundle savedInstanceState) {
    sendNotification();
    ....
}

public void sendNotification() {
    Calendar cal = Calendar.getInstance();
    Calendar newDay = Calendar.getInstance();

    cal.set(Calendar.HOUR_OF_DAY, 9);// 9 am
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    if (cal.getTimeInMillis() < newDay.getTimeInMillis())
        cal.add(Calendar.DATE, 1);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, NotificationReceiver.class);
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0 , intent , 0 ) ;
    // keep repeating the alarm every 5 mins
    if(alarmManager != null ) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 300000 , pendingIntent1);
    }
}

基本上,正在发生的是,如果我没有在9点打开设备,就不会在9点时发出警报,这可能要晚10分钟,有时甚至更多! 如果我在9点打开设备并收到通知,它必须在5分钟后重新发送通知给我,并且这种情况没有发生,如果在这段时间后打开应用程序,我只会收到通知

过去一周,我一直在寻找解决方案,但找不到任何有用的答案。

1 个答案:

答案 0 :(得分:0)

也许尝试使用setExact()方法?

public void sendNotification() {
Calendar cal = Calendar.getInstance();
Calendar newDay = Calendar.getInstance();

cal.set(Calendar.HOUR_OF_DAY, 9);// 9 am
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

if (cal.getTimeInMillis() < newDay.getTimeInMillis())
    cal.add(Calendar.DATE, 1);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0 , intent , 0 ) ;
// keep repeating the alarm every 5 mins
if(alarmManager != null ) {
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)    ///Just used setExact(), it will make alarm go off when time reaches, seconds dont count.

修改

class AlertReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {

     //track your alarms here. Make sure to keep request codes unique. The intent here can be used to recieve the data you have sent from your activity/fragment. 

     setReminder()//pass whatever data you wish
   }


        private fun setReminder(calendar: Calendar, title: String, text: String, reqCode: Int) {

    val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    val noteText = text//text.replace("[ ]", "☐").replace("[x]", "☑")

    val intent = Intent(this, AlertReceiver::class.java)  //transfer data to notification class
    intent.putExtra("title", title)
    intent.putExtra("text", noteText)
    intent.putExtra("uniqueID", reqCode)

    val pendingIntent: PendingIntent = PendingIntent.getBroadcast(this, reqCode, intent, 0)
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
}

 }