본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[Android/안드로이드] Alarm Service 에 대해 알아본다.

by 돼지왕 왕돼지 2012. 2. 18.
반응형


안녕하세요 돼지왕 왕돼지입니다.
오늘은 AlarmService 에 대해 알아보겠습니다.


Alarm Service 에 대해 소개바람.

 
알람은 운영체제가 관리합니다. 응용 프로그램이 종료된 상태에서도 알람은 동작하며 시간이 되면 응용 프로그램을 가동시키기도 합니다.
심지어 장비가 슬립 상태이더라도 장비를 깨워 응용 프로그램을 실행시킨다는 면에서 아주 강력한 녀석이죠.
한번 설정한 알람이 취소되는 경우는 장비를 재부팅(Rom reset류)할 때 뿐입니다.
장비의 전원을 끄거나 명시적으로 알람을 취소하지 않는 한 알람은 어떤 조건에서도 정확히 동작합니다.
 
그래서 모닝콜, 약속 알람, 예약 다운로드 & 업로드, 디스크 조각모음, 바이러스 체크 등에 사용됩니다. 



관련 API 들좀 나열해보삼.

 
Context.getSystemService(Context.ALARM_SERVICE);
 
void set (int type, long triggerAtTime, PendingIntent operation)
void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
  -> set은 딱 한번 동작하는 알람이고, setRepeating은 주기를 정하고 반복적으로 동작하는 알람입니다.
 
 
<type> - 예약시간을 해석하는 방법과 예약 시간에 장비가 슬립 모드일 때 기동 여부 지정
 

 값 설명 
 RTC 세계 표준시 ( UTC ) 로 System.currentTimeMillis() 의 value. 
 RTC_WAKEUP 위와 같되 장비를 깨우는 작업 포함.
 ELAPSED_REALTIME 부팅 이후의 경과시간으로 지정. System.elapsedRealtime() 의 value 
 ELAPSED_REALTIME_WAKEUP 위와 같되 장비를 깨운다. 
 
 




샘플코드를 줘보삼.


// Alarm 설정
 
AlarmManager am = (AlarmManager)getSystemService (Context.ALARM_SERVICE);
Intent intent;
PendingIntent sender;
 
// 1회
intent = new Intent(.this, .class);
sender = PendingIntent.getBroadcast(.this, 0, intent, 0);
 
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
 
am.set(AlarmManger.RTC, calendar.getTimeInMillis(), sender);
 
// 여러번
am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealTime(), 6000, sender);
am.cancel(sender);





반응형

댓글