안드로이드 개발 질문/답변
(글 수 45,052)
public void alarm(long time) {
Date t = new Date();
t.setTime(time + 60 * 1000);
Intent intent = new Intent(mContext.getApplicationContext(),
WorkOrderAlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(mContext, 0, intent, 0);
AlarmManager am = (AlarmManager) mContext
.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, t.getTime(), sender);
}
제가 이런식으로 시간을 받아들여서 1분후에 알람이 울리게끔 알람에 등록했했습니다. 그리구 PendingIntent로 WorkOrderAlarmReceiver로 보낸다음 리시버에서는 Notification으로 통지 하게끔 구현해놨습니다.
여기서 제가 다음으로 하려는게.. 알람이 등록된후에 1분이 지나면 알람이 울리자나용~
그중간에 알람이 울리기전에 버튼을 클릭하면 그 알람이 종료되는 것을 구현하고 싶은데요..
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
am.cancel() <------ 이 함수가 알람을 종료하는것 같은데..
어떻게 사용해서 종료하죠?ㅠ 음..잘 모르겠어요..;; 고수분들 도와주세요~ㅠㅠ




AlarmManager mManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mManager.cancel(pi);
getBroadcast() 의 requestCode 는 알람을 지정할때 줬던 requestCode 와 동일해야 합니다.
만약 알람을 지정할때 requestCode의 값을 0으로 줬다면 해지할때도 0으로 줘야죠;;
알람이 한개일경우에는 큰 문제가 안되는데, 여러개일 경우에는 리퀘스트코드값을 다르게 주면 각각 다른 알람이 되니까요 ^^
해지할때도 해당 리퀘스트 코드를 줘야 합니다.