메뉴 건너뛰기

조회 수 2142 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

widget_provider.xml에서 조정할 수 있는 android:updatePeriodMillis 속성은 최소 30분이므로 이보다 작은 값 (30 * 60 * 1000)보다 작은 수 입력해도 30분마다 업데이트 메시지를 받게 된다. 이보다 짧은 주기로 업데이트를 수행하기 위한 방법 중 하나는 AlarmManager가 있다.

// WidgetProvider.java
public class WidgetProvider extends AppWidgetProvider
{
	/**
		5000 msec 간격으로 알람을 발생합니다.
	*/
	private static final int WIDGET_ALARM_INTERVAL = 5000;
	private static PendingIntent pendingIntent;
	private static AlarmManager alarmManager;

	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = null;
		
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		action = intent.getAction();

		if (action == null)
		{
			// 별 의미는 없지만 만약을 대비해 넣음
		}
		else if (android.appwidget.action.APPWIDGET_UPDATE.equals(action)) // 위젯 업데이트 인텐트를 수신했을 때
		{
			// 현재의 유닉스시간[msec]으로부터 5000[msec] 후에 알람을 발생시킴
			long nextTime = System.currentTimeMillis() + WidgetProvider.WIDGET_UPDATE_INTERVAL;

			Log.i("WidgetProvider.onReceive", "android.appwidget.action.APPWIDGET_UPDATE 수신함");

			// 이전에 생성된 알람이 있는 경우 이를 지우고 앞으로 작동시킬 새 알람을 만듦
			if (WidgetProvider.pendingIntent != null)
			{
				WidgetProvider.pendingIntent.cancel();
				WidgetProvider.pendingIntent = null;
			}
			if (WidgetProvider.alarmManager != null)
			{
				WidgetProvider.alarmManager.cancel();
				WidgetProvider.alarmManager = null;
			}
			// 이전 알람 제거 작업 끝

			// android.appwidget.action.APPWIDGET_UPDATE가 호출될 때 onReceive 함수가 받은 인텐트를 다음 Alaram 호출 때 그대로 전달함
			WidgetProvider.pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

			// 다음 Alarm이 작동될 시각을 지정하여 새 AlarmManager를 얻기
			WidgetProvider.alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
			WidgetProvider.alarmManager.set(AlarmManager.RTC, nextTime, WidgetProvider.pendingIntent);
		}
		else if (android.appwidget.action.APPWIDGET_DISABLED.equals(action))
		{
			Log.i("WidgetProvider.onReceive", "android.appwidget.action.APPWIDGET_DISABLED 수신함");

			// 이전에 생성된 알람이 있는 경우 이를 지움
			if (WidgetProvider.pendingIntent != null)
			{
				WidgetProvider.pendingIntent.cancel();
				WidgetProvider.pendingIntent = null;
			}
			if (WidgetProvider.alarmManager != null)
			{
				WidgetProvider.alarmManager.cancel();
				WidgetProvider.alarmManager = null;
			}
			// 이전 알람 제거 작업 끝
		}
	}
  }

public void set (int type, long triggerAtTime, PendingIntent operation)

알람을 생성하는 기본 함수로서 첫 번째 매개변수는 알람의 종류를 지정합니다.

AlarmManager.ELAPSED_REALTIME
단말기가 부팅된 후 경과 시각을 기준으로 알람을 작동합니다. 이 때 triggerAtTime 매개변수로 전달할 예약 시각은 SystemClock.elapsedRealtime() + (알람을 원하는 특정 시점) 입니다.
AlarmManager.ELAPSED_REALTIME_WAKEUP
단말기가 부팅된 후 경과 시각을 기준으로 알람을 작동합니다. 단, 단말기가 대기 모드인 경우는 알람을 위해 경과 시간을 세지 않습니다. 이 때 현재 시각은 SystemClock.elapsedRealtime() 함수로 얻습니다.
AlarmManager.ELAPSED_RTC
단말기의 현지 시각을 기준으로 알람을 작동합니다. 이 때 triggerAtTime 매개변수로 전달할 예약 시각은 System.currentTimeMillis() 함수로 얻습니다.
AlarmManager.ELAPSED_RTC_WAKEUP
단말기의 현지 시각을 기준으로 알람을 작동합니다. 단, 단말기가 대기 모드인 경우는 알람을 위해 경과 시간을 세지 않습니다. 이 때 triggerAtTime 매개변수로 전달할 예약 시각은 System.currentTimeMillis() + (알람을 원하는 특정 시점) 입니다.

반복 실행 설정을 위한 함수는 다음과 같습니다.

public void setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation);

처음 1회는 triggerAtTime에서 지정한 시점에서 알람을 발생하고 이후 interval에서 지정한 간격마다 알람을 발생합니다. setRepeating 함수는 정확한 시각에 알람을 발생시키며 배터리 소모가 상대적으로 많습니다. 정확성보다는 대강의 간격을 두고 알람을 얻기를 원한다면 아래 함수를 사용하면 됩니다.

public void setInexactRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)



List of Articles
번호 제목 날짜 조회 수
177 ScrollView의 활용 2015.07.16 6530
176 특정 폴더에서 오래된 파일 삭제하기 2015.07.16 6767
175 네트워크를 통해 받은 이미지를 파일로 저장하고, 크기 조절해서 불러오기 2015.07.16 6155
174 화면 해상도에 관계없는 레이아웃(Layout) 만들기 file 2015.07.16 8641
173 화면 회전에 따른 애니메이션 효과 구현하기 2015.07.16 8055
172 이미지의 Orientation를 체크해서 이미지 회전하기 2015.07.16 7658
171 이미지 버튼(ImageButton) 만들기 2015.07.16 7114
170 체크 박스(CheckBox)의 이미지 바꾸기 2015.07.16 6398
169 사용자 정의 팝업창 띄우기 2015.07.16 6337
168 EditText의 글자 수 제한 걸기 2015.07.16 13881
167 옵션 메뉴 동적으로 생성하기 2015.07.16 6926
166 네트워크 상태 변화 감지하기(BroadcastReceiver 사용) 2015.07.16 9935
165 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (1) file 2015.07.16 6726
164 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (2) file 2015.07.16 7292
163 푸쉬 알림 기능. GCM (Google Cloud Messaging) 사용하기 (3) file 2015.07.16 6267
162 탭 뷰에 탭 추가하기, 아이콘 넣기 file 2015.07.16 9360
161 스토리보드 짜는 방법 file 2015.07.16 15419
160 [안드로이드] Activity에 대해서 file 2015.07.16 6767
159 [안드로이드] 레이아웃의 기본1 file 2015.07.16 6962
158 [안드로이드] 레이아웃의 기본2 file 2015.07.16 7071
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved