所以我试图制作一个假设在特定时间发布的AlarmManager
。问题是,当我设置时间时,tt不起作用。我认为主要的问题是,在我更改了Calendar
上的时间后,在日志中对象值“时间”显示了吗?
timeOff = Calendar.getInstance();
System.out.println(timeOff);
timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
timeOff.set(Calendar.HOUR_OF_DAY, 16);
timeOff.set(Calendar.MINUTE, 30);
timeOff.set(Calendar.SECOND, 0);
System.out.println(timeOff);
System.out.println("TIME:: "+timeOff.getTimeInMillis());
Intent intent = new Intent(TabsActivity.this, Receiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(TabsActivity.this, 1122, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(am.RTC_WAKEUP, timeOff.getTimeInMillis(),timeOff.getTimeInMillis()+ 1000,pendingIntent);// + 604800000L,pendingIntent);
日志显示: 改变之前:
java.util.GregorianCalendar[time=1406899765159,areFieldsSet=true,lenient=true,zone=Europe/Riga,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=7,WEEK_OF_YEAR=31,WEEK_OF_MONTH=1,DAY_OF_MONTH=1,DAY_OF_YEAR=213,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=29,SECOND=25,MILLISECOND=159,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
变更后:
java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,zone=Europe/Riga,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=7,DAY_OF_YEAR=219,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=30,SECOND=0,MILLISECOND=159,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
如您所见 -
Time=?
所以我的问题是,我做错了什么,为什么在我设定时间之后,它没有表现出来。即使我尝试注销普通timeOff.getTimeInMillis()
,它也根本没有显示任何内容。
将timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY)
更改为timeOff.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY)
答案 0 :(得分:0)
因为calendar.add()方法是抽象的,所以可以像这样使用
Calendar timeOff = Calendar.getInstance();
System.out.println(timeOff);
timeOff.add(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
timeOff.getTime()
//System.out.println(timeOff.getTime());
timeOff.set(Calendar.HOUR_OF_DAY, 16);
timeOff.set(Calendar.MINUTE, 30);
timeOff.set(Calendar.SECOND, 0);
System.out.println(timeOff);
System.out.println("TIME:: "+timeOff.getTimeInMillis());