메뉴 건너뛰기

조회 수 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 Activity Data Transfor/ 액티비티 이동간에 데이터 전송하기 file 2016.06.07 7676
256 Activity Switching / 안드로이드 액티비티 전환 / 화면 전환 file 2016.06.07 8311
255 Android Login and Registration with PHP, MySQL and SQLite file 2015.07.16 14178
254 Android Navigation Drawer API 공개! 디자인 가이드 살펴보기 file 2015.07.29 8141
253 Android Push GCM 서버 구성 하기(3) file 2015.12.14 6388
252 Android Push GCM 프로젝트 앱 적용 하기(2) file 2016.03.18 8953
251 android SMS 리시버 2015.06.29 6871
250 Android Studio에서 SQLCipher 라이브러리 추가 방법 file 2018.10.02 1776
249 Android TIP] strings.xml 에서 특수문자 사용하기 2015.12.15 6629
248 Android 간단한 회원 가입 폼 만들기 for Mac (PHPMyAdmin 이용) file 2015.07.10 10508
247 Android 와 JSP 간 파라미터 암복호화 (1) file 2016.05.26 7474
246 Android 와 JSP 간 파라미터 암복호화 (2) 2016.05.26 7735
245 Android 와 JSP 간 파라미터 암복호화 (3) file 2016.05.26 8088
244 android.support.v4.content.FileProvider not found file 2020.12.14 308
243 AndroidManifest에 선언한 메타데이터(meta-data) 가져오기 2016.06.10 9313
242 Android] Fragment 내부의adapter에서 startActivity 하기 2015.12.15 6487
241 Android] 안드로이드 홈 디렉토리 알아내기 2015.12.15 6895
240 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1619
239 App 실행 file 2021.03.31 244
238 CSS3 Rounded Corner, 그림자 효과 사용하기 file 2014.09.04 6760
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 13 Next
/ 13

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved