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

时间:2014-08-08 02:22:34

标签: android alarmmanager

奇怪的是,我的警报管理员正在立即调用待处理的意图,即使我已经提出一个条件,如果当前时间大于一天后的警报时间。

我有闹钟管理员每天晚上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);
            }

日志以毫秒为单位显示第二天,但会立即触发。这可能是什么问题?

2 个答案:

答案 0 :(得分:0)

尝试使用这种方式,如果您遇到任何问题,请告诉我

    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);
        }

答案 1 :(得分:0)

我建议检查警报管理器是否为空,然后在设置警报之前取消警报,这花了我一些时间示例

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