0

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

 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,然后现在也运行代码

请帮帮我

提前谢谢

像这样的同样问题 我如何在android中使用警报管理器调用过去的警报?

4

2 回答 2

2
 if (calendar1.before(cal_now)) {// if its in the past increment
                    calendar1.add(Calendar.DATE, 1);
}
于 2013-02-25T10:22:27.523 回答
0

只需添加这一行我认为你会得到正确的时间:---

Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
于 2013-01-08T11:06:39.373 回答