본문 바로가기

Application Programming Interface/Android

위젯 업데이트 주기 빠르게 하기

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

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)