这是我在点击我的设置闹钟按钮时使用的代码
alarmOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,timePicker.getHour());
calendar.set(Calendar.MINUTE,timePicker.getMinute());
int hourString = timePicker.getHour();
int minuteString = timePicker.getMinute();
//intent.putExtra("extra","alarm_on");
setAlarmTxt("Alarm set to "+ hourString+":"+minuteString);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
//alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),0,pendingIntent);
// sendBroadcast(intent);
}
});
当我使用此代码时,它正在运行......
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealti
me(),pendingIntent);
答案 0 :(得分:0)
使用RTC_WAKEUP来使用系统当前时间和ELAPSED_REALTIME_WAKEUP来指定自设备启动时间以来经过的时间间隔的时间。
即,
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealti
me(),pendingIntent);
或
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
有关详细信息,请参阅this