如果给出过去时间,报警管理器现在正在工作

时间:2013-01-08 11:01:37

标签: android scheduling alarmmanager

从时间选择器中抽出时间,当我设置调度时间来完成工作时,它会产生问题

 SharedPreferences prefs = getSharedPreferences("CONSTANT_FILE_NAME",Context.MODE_WORLD_WRITEABLE);
             SharedPreferences.Editor editor=   prefs.edit();
             editor.putLong("timepickerhour", timePicker.getCurrentHour());
                editor.putLong("timepickerminute", timePicker.getCurrentMinute());
                editor.putLong("milis", milis);
                editor.commit();



    SharedPreferences prfs = getSharedPreferences("CONSTANT_FILE_NAME",Context.MODE_PRIVATE);
                h =prfs.getLong("timepickerhour",0); 
                m =prfs.getLong("timepickerminute",0); 
                r =prfs.getLong("milis",0);
                AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent intent1 = new Intent(Scheduling.this, Feedback.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(Scheduling.this, 0,
                  intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(Calendar.HOUR_OF_DAY, (int) h);
                calendar1.set(Calendar.MINUTE, (int) m);
                am.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(),
                        r, pendingIntent);
当我现在给出过去时间时面临问题,就像现在时间是下午4:25如果我在下午2:25设定时间那么那个调度任务在现场运行...... 也问题,如果现在时间是下午4:38,我设置时间4:38 am然后也运行代码现在

请帮帮我

事先提前

像这样的问题 How can i invoke the alarm for the past time using alarm manager in android?

2 个答案:

答案 0 :(得分:2)

 if (calendar1.before(cal_now)) {// if its in the past increment
                    calendar1.add(Calendar.DATE, 1);
}

答案 1 :(得分:0)

只需添加此行我认为您将获得正确的时间:---

Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());