警报管理器未按预期触发

问题描述 投票:0回答:1

我在应用程序中遇到的所有警报都存在问题。我的应用程序的整个想法取决于在特定时间触发警报(如果出现问题,用户将付钱(信用))。这是我为解释我的问题而编写的警报之一:

@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分钟后重新向我发送通知,但这不会发生,如果我在这段时间后打开应用程序,我只会收到通知。

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

android alarmmanager
1个回答
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)
}

 }
© www.soinside.com 2019 - 2024. All rights reserved.