메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
257 Effects - Animate() 메서드 (여러가지 효과 동시 처리) file 2014.10.16 30619
256 안드로이드와 mysql 연동시키기. php 와 json 사용 file 2015.07.16 24487
255 [DB] 서버/클라이언트 소켓 통신하기 2015.07.13 20566
254 월별 캘린더에 일정 입력 및 조회 기능 리스트로 추가하기 file 2015.07.16 18552
253 스토리보드 짜는 방법 file 2015.07.16 15419
252 카카오톡 대화내용 가져오기(sqlite3, chat_logs) file 2016.05.26 15116
251 간단한 mp3 플레이어 만들기 , 음악넣기 , 노래재생 file 2016.06.07 14623
250 TextureView를 이용한 카메라 Preview 좌우 반전 2015.06.10 14211
249 Android Login and Registration with PHP, MySQL and SQLite file 2015.07.16 14178
248 블루투스(Bluetooth) 통신에 대해 알아보자 file 2015.07.26 14047
247 사진찍기 및 앨범 에서 사진 가져오기!!! 2014.08.28 13889
246 EditText의 글자 수 제한 걸기 2015.07.16 13881
245 [DB]Android - DB 연동 기술 정리 2015.07.13 13798
244 노티피케이션(Notification) 사용법 / Notification.Builder , NotificationManager file 2016.06.10 13470
243 안드로이드] 페이스북 같은 슬라이드 메뉴 만들기 file 2015.12.15 12536
242 안드로이드용 채팅프로그램 클라이언트(java), 서버(c#) 소스 file 2016.05.19 11722
241 안드로이드에서 JSP 를 사용하여 mysql 연동하기 2015.07.16 11685
240 WIFI 신호세기 강도 측정하기 2014.08.28 11243
239 블루투스 및 비콘 관련 정리 2015.07.26 10828
238 Android 간단한 회원 가입 폼 만들기 for Mac (PHPMyAdmin 이용) file 2015.07.10 10508
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved