달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
2012. 8. 30. 23:14

Android - AlarmManager 안드로이드 이야기2012. 8. 30. 23:14

728x90
반응형

Android - AlarmManager

원본 출처:http://pheadra.tistory.com/entry/Alarm

○ 알람의 정의
- 미리 지정해 놓은 시간에 이벤트를 발생시키는 시스템 장치
- 장래의 특정 시점이나 일정 시간 후에 작업을 등록하고 싶을때 알림을 사용
- 알람은 운영제체가 관리하며 응용 프로그램 외부에서도 설정 가능.
- 응용 프로그램이 종료된 상태에서도 알람은 동작함
- 장치가 슬립 상태이더라도 장비를 깨워 응용 프로그램을 실행 시킬수 있다.
- 알람은 일종의 약속 시간 알림 기능을 보여주는데 현재 시점이후 일정 시간이 경과했을 때 BR을 호출한다.

○ 알람 메니지 얻기
AlarmManager am = context.getSystemService(Context.ALARM_SERVICE);

○ 알람 등록 함수

// 딱 한번
void set(int type, long triggerAtTime, PendingIntent operation)
// 주기를 정해 놓고 반복적
void setReating(int type, long tirggerAtTime, long interval, PendingIntent operation)

※ The kind of type
RTC_WAKEUP (0x0000 0000)
RTC (0x0000 0001)
ELAPSED_REALTIME_WAKEUP (0x0000 0002)
ELAPSED_REALTIME (0x0000 0003)

○ 알람 취소 함수
void cancel(PendingIntent operation)


예제 소스

AlarmTest.class
  1. public class AlarmTest extends Activity {
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. Intent intent = new Intent(AlarmTest.this, AlarmReceiver.class);
  5. PendingIntent sender = PendingIntent.getBroadcast(AlarmTest.this, 0,
  6. intent, 0);
  7. Calendar calendar = Calendar.getInstance();
  8. calendar.setTimeInMillis(System.currentTimeMillis());
  9. calendar.add(Calendar.SECOND, 10);
  10. am.set(AlarmManager.RTC, calendar.getTimeInMillis(), sender);
  11. }
  12. }

예제 소스2
SetAlarm.java
  1. private OnClickListener mOneShotListener = new OnClickListener() { // 버튼 이벤트 리스너
  2. public void onClick(View v) {
  3. Intent intent = new Intent(SetAlarm.this, AlarmReceiver.class);
  4. PendingIntent appIntent =
  5. PendingIntent.getBroadcast(SetAlarm.this, 0, intent, 0);
  6. Calendar calendar = Calendar.getInstance();
  7. calendar.setTimeInMillis(System.currentTimeMillis());
  8. calendar.add(Calendar.SECOND, 0); // 0초 뒤에 발생..
  9. AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
  10. am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);
  11. }
  12. };

AlarmReceiver.java
  1. public class AlarmReceiver extends BroadcastReceiver {
  2. private int YOURAPP_NOTIFICATION_ID;
  3. @Override
  4. public void onReceive(Context context, Intent intent) {
  5. Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
  6. showNotification(context, R.drawable.alarm,
  7. R.string.alarm_message, R.string.alarm_message);
  8. }
  9. private void showNotification(Context context, int statusBarIconID,
  10. int statusBarTextID, int detailedTextID) {
  11. Intent contentIntent = new Intent(context, SetAlarm.class);
  12. PendingIntent theappIntent =
  13. PendingIntent.getBroadcast(context, 0, contentIntent, 0);
  14. CharSequence from = "Alarm Manager";
  15. CharSequence message = "The Alarm was fired";
  16. Notification notif =
  17. new Notification(statusBarIconID, null, System.currentTimeMillis());
  18. notif.setLatestEventInfo(context, from, message, theappIntent);
  19. NotificationManager nm =
  20. (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
  21. nm.notify(this.YOURAPP_NOTIFICATION_ID, notif);
  22. }
  23. }
Manifest.xml
< RECEIVER android:name=".AlarmReceiver" />
추가!!


여러 종류의 알람을 등록하고 싶을 때에는.. PendingIntent에 있는 request_code를 다르게 주면 된다.
Intent intent = new Intent(SetAlarm.this, AlarmReceiver.class);
PendingIntent appIntent =
PendingIntent.getBroadcast(SetAlarm.this, request_code, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);

그리고 만약 BR에서 Intent로 데이터를 받을때 잘 안된다면.. pendingIntent의 플래그를 FLAG_UPDATE_CURRENT로 바꿔보자..
PendingIntent appIntent =
PendingIntent.getBroadcast(SetAlarm.this, request_code, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Posted by 파이드라
728x90
반응형
:
Posted by mapagilove