메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
237 HTML5로 나만의 비디오 플레이어 스킨 만들기 -2- JavaScript file 2014.09.04 6312
236 HTML5로 나만의 비디오 플레이어 스킨 만들기 -3- JavaScript file 2014.09.04 6288
235 HTML5 드래그 앤 드롭 하기 Drag and Drop file 2014.09.04 7748
234 HTML5 Web Storage -01- file 2014.09.04 5587
233 HTML5 Form 공부하기 -1- file 2014.09.04 5841
232 HTML5 Form 공부하기 -2- file 2014.09.04 6329
231 HTML5로 게임 만들기 워밍업 file 2014.09.04 6063
230 HTML5 Better semantic tags file 2014.09.04 5952
229 HTML5 Geolocation (구글 지도에 현위치 표시하기) file 2014.09.04 6810
228 jQuery ajax post 요청 text 응답 2014.10.16 6702
227 JSON(JavaScript Object Notation) - jQuery Ajax - jQuery.getJSON() 메서드 (비동기적으로 JSON파일 로드) file 2014.10.16 6568
226 jQuery Ajax - jQuery.load() 메서드 (동적으로 원격 페이지 로드) file 2014.10.16 6409
225 트리뷰(TreeView) 컨트롤 file 2014.10.16 6722
224 Effects - Stop() 메서드 (애니메이션 효과 멈추기) file 2014.10.16 6225
223 Effects - Animate() 메서드 (여러가지 효과 동시 처리) file 2014.10.16 30628
222 Effects - SlideToggle() 메서드 (슬라이드 업/다운) file 2014.10.16 6760
221 Effects - SlideUp() 메서드 (슬라이드업) file 2014.10.16 5999
220 Effects - FadeIn() / FadeOut() 메서드 (서서히 보이기 및 숨기기) file 2014.10.16 6069
219 Effects - Show() / Hide() 메서드 (보이기 및 숨기기) file 2014.10.16 5957
218 Events - Unbind() 메서드 (이벤트 처리기 해제) file 2014.10.16 5749
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved