밑에 소스코드 입니다. 설정된 시간에 벨소리를 울리고 싶은데 밑에 큰 글씨로 해놓은 부분만 조금 수정 하면 될꺼같은데
자꾸 set 버튼 클릭시 바로 벨소리가 울리네요 ㅠㅠ 어떻게 해야될까요?
public class AlramActivity extends Activity
implements OnDateChangedListener, OnTimeChangedListener {
/*
* 알람관련 맴버 변수
*/
// 알람 메니저
private AlarmManager mManager;
// 설정 일시
private GregorianCalendar mCalendar;
//일자 설정 클래스
private DatePicker mDate;
//시작 설정 클래스
private TimePicker mTime;
private long nowTime;
/*
* 통지 관련 맴버 변수
*/
private NotificationManager mNotification;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//통지 매니저를 취득
mNotification = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//알람 매니저를 취득
mManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//현재 시각을 취득
mCalendar = new GregorianCalendar();
Log.i("HelloAlarmActivity",mCalendar.getTime().toString());
//셋 버튼, 리셋버튼의 리스너를 등록
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.set);
b.setOnClickListener (new View.OnClickListener() {
public void onClick (View v) {
setAlarm();
}
});
Button c = (Button)findViewById(R.id.reset);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resetAlarm();
}
});
//일시 설정 클래스로 현재 시각을 설정
mDate = (DatePicker)findViewById(R.id.date_picker);
mDate.init (mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), this);
mTime = (TimePicker)findViewById(R.id.time_picker);
mTime.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
mTime.setCurrentMinute(mCalendar.get(Calendar.MINUTE));
mTime.setOnTimeChangedListener(this);
mCalendar = new GregorianCalendar();
Log.i("HelloAlarmActivity",mCalendar.getTime().toString());
nowTime = mCalendar.getTimeInMillis();
}
//알람의 설정
private void setAlarm() {
if(nowTime >= mCalendar.getTimeInMillis()){
Toast.makeText(AlramActivity.this, "입력한 날짜는 현재 날짜보다 이전입니다.", Toast.LENGTH_SHORT).show();
return;
}
mManager.set(AlarmManager.RTC_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent());
}
//알람의 해제
private void resetAlarm() {
mManager.cancel(pendingIntent());
}
//알람의 설정 시각에 발생하는 인텐트 작성
private PendingIntent pendingIntent() {
Intent i = new Intent(getApplicationContext(), AlramActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);
return pi;
설정된 시간을 위해 제가 추가한 소스 부분
/*
int icon = R.drawable.icon;
String tickerText = "알림";
long when = System.currentTimeMillis();
Notification noti = new Notification(icon, tickerText, when);
noti.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");//ringURI;
Log.v("LEE", "mNotification11");
noti.setLatestEventInfo(Alram.this,"111", "222", pi);
mNotification.notify(1234, noti); // NOTIFICATION_ID의 고유 ID를 가지는 notification을 표시합니다.
Log.v("LEE", "mNotification22");
return pi;
*/
}
//일자 설정 클래스의 상태변화 리스너
public void onDateChanged (DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set (year, monthOfYear, dayOfMonth, mTime.getCurrentHour(), mTime.getCurrentMinute());
Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
}
//시각 설정 클래스의 상태변화 리스너
public void onTimeChanged (TimePicker view, int hourOfDay, int minute) {
mCalendar.set (mDate.getYear(), mDate.getMonth(), mDate.getDayOfMonth(), hourOfDay, minute);
Log.i("HelloAlarmActivity",mCalendar.getTime().toString());
}
}
알람을 등록한 시간이 곧 현재시간이기 때문에 울리는 거죠?
저역시 알람을 처음 구현할때 시작과 동시에 울린듯 하네요;;;
그래서 전 칼렌다에 1분을 더 더했습니다.
mCalendar.add(Calendar.MINUTE, 1);
요렇게요 ^^
저상태에서 설정하면 1분 후에 울리겠죠 ㅎㅎㅎ
그리고 전 TimePicker 를 사용해서 설정하고픈 시간을 사용자가 고른후에 알람에 set했습니다.
mTime = (TimePicker)findViewById(R.id.time_picker);
mCalendar.add(Calendar.MINUTE, 1);
mTime.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
mTime.setCurrentMinute(mCalendar.get(Calendar.MINUTE));
mTime.setOnTimeChangedListener(this);
public PendingIntent pendingIntent(int requestCode){
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("requestCode", requestCode);
PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pi;
}
//시각 설정 클래스의 상태변화 리스너
public void onTimeChanged(TimePicker view, int hourOfDay, int minute){
Log.e(Com.TAG, "시간변경");
mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
// Log.e(Com.TAG,mCalendar.getTime().toString());
}
public void setAlarm(){
sender = pendingIntent(m_requestCode);
// 알람5초후
// Calendar cal = Calendar.getInstance();
// cal.setTimeInMillis(System.currentTimeMillis());
// cal.add(Calendar.SECOND, 5);
m_alarmSettingTime = mCalendar.getTimeInMillis();
if(Com.m_alarmRepeat == 0)
mManager.set(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, sender);
else if(Com.m_alarmRepeat == 1)
mManager.setRepeating(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, 1000*60*5, sender);
else if(Com.m_alarmRepeat == 2)
mManager.setRepeating(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, 1000*60*60, sender);
else if(Com.m_alarmRepeat == 3)
mManager.setRepeating(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, 1000*60*60*24, sender);
else if(Com.m_alarmRepeat == 4)
mManager.setRepeating(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, 1000*60*60*24*7, sender);
}




public AlarmManager mManager;// 알람 메니저
public GregorianCalendar mCalendar;// 설정 일시
public PendingIntent sender;
public int m_requestCode;
public long m_alarmSettingTime;
mCalendar.add(Calendar.MINUTE, 1);
sender = pendingIntent(m_requestCode);
m_alarmSettingTime = mCalendar.getTimeInMillis();
mManager.set(AlarmManager.RTC_WAKEUP, m_alarmSettingTime, sender);
public PendingIntent pendingIntent(int requestCode){
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("requestCode", requestCode);
PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pi;
}