我正在开发警报应用程序,我需要在所有日子里同时重复调用警报。我正在使用代码来调用警报,
c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,10);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(HomeActivity.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(HomeActivity.this, 123123, intent, 0);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis(), (24*60*60*1000), pendingIntent);
通过使用此代码,我只在上午10点调用当天,而不是所有日子。还有另一种方法吗?
答案 0 :(得分:1)
更新时间,每隔5分钟响铃:
Intent intent = new Intent(this, this.getClass());
PendingIntent pendingIntent =
PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long currentTimeMillis = System.currentTimeMillis();
long nextUpdateTimeMillis = currentTimeMillis + 5 * DateUtils.MINUTE_IN_MILLIS;
Time nextUpdateTime = new Time();
nextUpdateTime.set(nextUpdateTimeMillis);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
设置闹钟每天工作时间(例如,上午8点至晚上18点)
// if not the time to ring, set to the next day
if (nextUpdateTime.hour < 8 || nextUpdateTime.hour >= 18)
{
nextUpdateTime.hour = 8;
nextUpdateTime.minute = 0;
nextUpdateTime.second = 0;
nextUpdateTimeMillis = nextUpdateTime.toMillis(false) + DateUtils.DAY_IN_MILLIS;
}
else{
// call your alarm
}
答案 1 :(得分:0)
在班级MyAlarmService
中,当闹钟开启时,请重置闹钟。在设置闹钟时,您甚至可以在意图中发送MyAlarmService
的值(可能是为了停止闹钟)