我用过
Calendar c = Calendar.getInstance();
c.set(year, month - 1, day, hourOfDay, minute);
boolean b = SystemClock.setCurrentTimeMillis(c.getTimeInMillis());
设置系统时间,它的工作,但不能设置超过timeInMillis的Integer.MAX_VALUE,
例如:2016-03-09 12:34:00(成功); 2150-03-09-12:34:00(失败)。
在android源代码中,Settings - DateTimeSettings用于getSystemService(Context.ALARM_SERVICE))。setTime,我也尝试用它来设置时间,但是有相同的错误。
static void setDate(Context context, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
long when = c.getTimeInMillis();
if (when / 1000 < Integer.MAX_VALUE) {
((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).setTime(when);
}
}
日志:
I/wpa_supplicant(461): [CTRL_IFACE]wlan0: SIGNAL_POLL
D/SystemClock(4186): Setting time of day to sec=1533218663
W/SystemClock(2005): time going backwards: prev 1884443673247396938(ioctl) vs now -526088270617409983(ioctl), tid=2005
I/PushLogSC2559(1190): [ReceiverDispatcher-91]read to Send:DA(activity/null:-1)
D/dalvikvm(1253): GC_FOR_ALLOC freed 373K, 20% free 3353K/4176K, paused 20ms, total 21ms
V/AlarmClock(4152): No next alarm
V/AlarmClock(4152): AlarmInitReceiver finished
谁能帮助我,谢谢!
答案 0 :(得分:0)