简体   繁体   English

警报管理器在Android中立即触发

[英]Alarm Manager is triggered immediately in Android

Strangely, my Alarm Manger is calling the pending intent immediately, even though I have put a condition if the current time is greater than alarm time run one day later. 奇怪的是,即使当前时间大于一天后运行的警报时间,即使我已经提出条件,我的警报管理器也会立即调用待处理的意图。

I have alarm Manger to trigger everyday at 10PM. 我有警报管理器,每天晚上10点触发。

  Intent startServiceIntent = new Intent(this, numbers.class);
  this.startService(startServiceIntent);

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(res.getString(R.string.hoursvalue))); //10
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.AM_PM, Calendar.PM);
            cal.set(Calendar.MILLISECOND, 0);

            long alarmtime = cal.getTimeInMillis();

            Log.e("ALarm","Time"+alarmtime);

            if (currenttime > alarmtime)
            {
                alarmtime = alarmtime + AlarmManager.INTERVAL_DAY;
                Log.e("Current Time","Greater"+alarmtime);
            }

            if (currentapiVersion >= 19)
            {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent2 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
                PendingIntent pi = PendingIntent.getService(this, 741258963, intent2, 0);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
            }
            else if (currentapiVersion >= 16 && currentapiVersion < 19)
            {
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent3 = new Intent(this, ServiceForLoadingOnlineNumbers.class);
                PendingIntent pi = PendingIntent.getService(this, 741258963, intent3, 0);
                am.setRepeating(AlarmManager.RTC_WAKEUP, alarmtime, AlarmManager.INTERVAL_DAY, pi);
            }

The Log shows the next day in milliseconds correctly but triggers immediately. 日志会正确显示第二天(以毫秒为单位),但会立即触发。 What could be the issue here? 这里可能是什么问题?

Try to use this way and let me know if you face any issues 尝试使用这种方式,让我知道您是否遇到任何问题

    Date dat = new Date();// initializes to now
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);

        Toast.makeText(mContext, "" + cal_now.getTime(), Toast.LENGTH_LONG)
                .show();
        ;
        cal_alarm.setTime(dat);
        cal_alarm.set(Calendar.HOUR_OF_DAY, 13);// set the alarm time
        cal_alarm.set(Calendar.MINUTE, 10);
        cal_alarm.set(Calendar.SECOND, 10);
        if (cal_alarm.before(cal_now)) {// if its in the past increment
            cal_alarm.add(Calendar.DATE, 1);
        }

I suggest checking if the alarm manager is null then canceling the alarm before setting the alarm this took me some time example 我建议检查警报管理器是否为空,然后在设置警报之前取消警报,这花了我一些时间

if(alarmManger != null){
    alarmManger.cancel(operation);
    alarmManger.setRepeating(alarmType,alarmtime,‌​AlarmManager.INTERVAL_DAY, operation);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM