2012년 1월 25일 수요일

[Android] 알람이 동작중인지 확인 하는법

제목 그대로 알람이 동작중인지 확인하여 동작 중이라면
알람을 재등록 하지 않는 다던가의 처리가 필요한 경우가 있다.
그럴 경우 다음과 같이 하면 알람이 등록 되었는지 확인 할 수 있다.

private boolean isAlarmActivated() 
{
boolean result;
PendingIntent pIntent;

Intent intentToSend = new Intent();
intentToSend.setAction("SOME_ACTION");

pIntent = PendingIntent.getService(getContext(), 0, intentToSend, PendingIntent.FLAG_NO_CREATE);

result = pIntent != null;

Logging.d(TAG, "[isAlarmActivated] " + result + " - " + pIntent);
   return result;
}

단, 주의해야 할 점이 몇가지 있다.
(아래 항목을 간과 한다면 계속 false 를 보게됨.)

1. 호출메소드를 잘 선택 할것
  - 밑줄친 부분의 메소드 명인데 PendingIntent를 처리할 주체가 어떤녀석이냐에 따라
PendingIntent.getActivity(context, requestCode, intent, flags);
PendingIntent.getActivities(context, requestCode, intents, flags);
PendingIntent.getBroadcast(context, requestCode, intent, flags);
PendingIntent.getService(context, requestCode, intent, flags);
  중에 호출할 메소드를 잘 선택해 주어야 한다.

2. 알람의 Intent는 동일하게 할 것
  - 밑줄친 메소드의 인자중 3번째 인자인 Intent 값의 경우 알람 생성시 지정한 값과 같아야(완전히 같아야 한다는게 아니고 동일한 인텐트로 인식 될 수 있으면 됨)  한다.

3. 알람 해제시 PendingIntent를 cancel() 해줄 것
 - 알람을 해제 할때는 PendingIntent.cancel() 을 호출해 주어야 한다. 그렇지 않으면 알람은 해제 되었는데 PendingIntent가 살아 있어 원하는 결과를 얻을 수 없게 된다. (알람 먼저 cancel하고 동일한 PendingIntent를 Cancel하면 된다)


댓글 없음:

댓글 쓰기