该代码中的问题是什么?我试图在上午11点发出警报,我在上午10点设置它,所以它应该在一个小时后发生,这是从未发生过的
public void schedulePayment(Context ctxt,int numOfDays) {
mgr = (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(ctxt, NotificationPayment.class);
PendingIntent pendingIntent = PendingIntent.getService(ctxt, 0, myIntent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
time.clear();
time.set(Calendar.HOUR_OF_DAY, 11);
time.set(Calendar.MINUTE, 1);
time.set(Calendar.SECOND, 1);
long limit_time = time.getTimeInMillis() ;
// if(time.before(Calendar.getInstance())){
// Log.d("add time ","extra");
// limit_time+=AlarmManager.INTERVAL_DAY;
// }
Log.d("in schedule ","done..."+limit_time);
mgr.setRepeating(AlarmManager.RTC_WAKEUP, limit_time,AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
}
答案 0 :(得分:2)
请勿拨打time.clear();
这是删除所有日历字段,例如日期。
答案 1 :(得分:1)
警报管理器永远不会因为你的limit_time(setRepeating中的第二个参数)小于currentTimeMillis而尝试。尝试使用System.currentTimeMillis + interval而不是limit_time