我每天都会重复闹钟。但它会有几秒或几分钟的错误。我怎样才能让它更准确?
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), notificationId, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
long startUpTime = calendar.getTimeInMillis();
if (System.currentTimeMillis() > startUpTime) {
startUpTime = startUpTime + 24*60*60*1000;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24*60*60*1000 , pendingIntent);
尝试添加
calendar.set(Calendar.SECOND,00);
和改变
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24*60*60*1000 , pendingIntent);
至
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);
1)获取时间形式的时间戳基础
calNow = Calendar.getInstance();
calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
if (calSet.compareTo(calNow) <= 0) {
//Today Set time passed, count to tomorrow
calSet.add(Calendar.DATE, 1);
}
2)每日设置警报
Intent intent = new Intent(AddAlarmNewActivity.this, OnAlarmReceive.class);
intent.putExtra("alarmTitle", mTitle.getText().toString());
intent.putExtra("alarmId", insertedId + "");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
(int)insertedId,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
targetCal.getTimeInMillis(),
24*60*60*1000,
pendingIntent);
Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);