AlarmManager
및 NotificationManager
과 BroadcastReceiver
입니다. 특정 날짜의 알람을 설정하면 처음으로 작동합니다. 그러나 날짜를 수정하고 확인 버튼을 클릭하면 알람이 즉시 작동합니다 ( ). 만료일 이후의 알람 간격 요일을 고정 된 시간으로 설정하고 싶습니다. 문제가 무엇입니까? 나는 그 순간 이해하지 못한다.날짜가 바뀌면 알림이있는 Android 알람이
confirmButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
//set alarm with expiration date
am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
Toast.makeText(fridgeDetails.this, "Alarm automatic set",
Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
public void setOneTimeAlarm() {
c.set(Calendar.HOUR_OF_DAY, 14);
c.set(Calendar.MINUTE, 49);
c.set(expiredYear, expiredMonth, expiredDay);
Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
}
});
는 AlarmService.java
public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(
Context.NOTIFICATION_SERVICE);
CharSequence from = "Check your fridge";
CharSequence message = "It's time to eat!";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,
"Keep Fridge", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
notif.defaults |= Notification.DEFAULT_SOUND;
notif.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(1, notif);
}
}
"homeactivity"와 같은 집 활동 및 활동 이름을 실행한다고 가정처럼 의도에 Acitivity 이름을 전달 문제는 완전히 있지만, FLAG_ONE_SHOT을 사용하는 것은이 경우에는 차선책입니다. AlarmManager에서 하나의 콜백 만 허용하기 때문입니다. 대신 0을 사용해보십시오. –