drcarter의 DevLog

특정 요일을 선택했으면 선택한 요일에 대해서만 알람을 울리고 선택된 요일이 없으면 설정된 시간에 한번만 알람이 울리도록 합니다. 전체적인 코드는 블로그에 올려두기 보다는 그냥 했던 방법을 적어 둘려고 합니다.


//알람 등록 및 취소

	private void registAlarm()
	{
		cancelAlarm();
		
		boolean[] week = { false, mTextRepeatSun.isSelected(), mTextRepeatMon.isSelected(), mTextRepeatTue.isSelected(),
				mTextRepeatWed.isSelected(), mTextRepeatThu.isSelected(), mTextRepeatFri.isSelected(),
				mTextRepeatSat.isSelected() };
		
		boolean isRepeat = false;
		int len = week.length;
		for (int i = 0; i < len; i++)
		{
			if (week[i])
			{
				isRepeat = true;
				break;
			}
		}

		// 알람 등록
		Intent intent = new Intent(this, AlarmReceiver.class);
		
		long triggerTime = 0;
		long intervalTime = 24 * 60 * 60 * 1000;// 24시간
		if(isRepeat)
		{
			intent.putExtra("one_time", false);
			intent.putExtra("day_of_week", week);
			PendingIntent pending = getPendingIntent(intent);
			
			triggerTime = setTriggerTime();
			
			mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, intervalTime, pending);
		}
		else
		{
			intent.putExtra("one_time", true);
			PendingIntent pending = getPendingIntent(intent);
			
			triggerTime = setTriggerTime();
			mAlarmManager.set(AlarmManager.RTC_WAKEUP, triggerTime, pending);
		}
		
		showToastMessage(getString(R.string.alarm_time_set_toast));
	}

	private PendingIntent getPendingIntent(Intent intent)
	{
		PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		return pIntent;
	}
	
	private void showToastMessage(String message)
	{
		if(mToast == null)
		{
			mToast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		}
		else
		{
			mToast.setText(message);
		}
		mToast.show();
	}
	
	private long setTriggerTime()
	{
		// current Time
		long atime = System.currentTimeMillis();
		// timepicker
		Calendar curTime = Calendar.getInstance();
		curTime.set(Calendar.HOUR_OF_DAY, this.mAlarmData.getHour(this));
		curTime.set(Calendar.MINUTE, this.mAlarmData.getMinute(this));
		curTime.set(Calendar.SECOND, 0);
		curTime.set(Calendar.MILLISECOND, 0);
		long btime = curTime.getTimeInMillis();
		long triggerTime = btime;
		if (atime > btime)
			triggerTime += 1000 * 60 * 60 * 24;
		
		return triggerTime;
	}

	private void cancelAlarm()
	{
		Intent intent = new Intent(this, AlarmReceiver.class);
		PendingIntent pending = getPendingIntent(intent);
		this.mAlarmManager.cancel(pending);
	}


//AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		Bundle extra = intent.getExtras();
		if (extra != null)
		{
			boolean isOneTime = extra.getBoolean("one_time");
			if (isOneTime)
			{
				AlarmDataManager.getInstance().setAlarmEnable(context, false);
                                // 알람 울리기.
			}
			else
			{
				boolean[] week = extra.getBooleanArray("day_of_week");

				Calendar cal = Calendar.getInstance();

				if (!week[cal.get(Calendar.DAY_OF_WEEK)])
					return;

                                // 알람 울리기.
			}
		}
	}
}


저는 이와 같이 만들어서 사용합니다.