스위치 위젯으로 상태를 저장하고 어플을 실행할때 저장된 상태에 따라 알람이 켜져있으면(1이 저장됨)
체크를 true로, 알람이 꺼져있으면(0이 저장됨) false로 하였는데요.
다음은 타이머가 실행/정지되는 부분이구요
어플을 킨 상태에서 알람 실행/중지는 되는데
어플을 키고 알람을 실행하고 어플을 종료하면
알람이 계속 발생하지만 다시 어플을 실행해서 알람을 중지하려하면 중지가 되지않고 계속 알람이 발생합니다.
어떻게 해야 다시 알람이 뜨지 않게 할 수 있나요?
as.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(as.isChecked()) start();
else stop();
}
});
public void start() {
task = new TimerTask(){ //timerTask는 timer가 일할 내용을 기록하는 객체
@Override
public void run() {
notifyMe();
}
};
timer= new Timer(); //timer생성
timer.schedule(task, 2000, 2000);
try {
FileOutputStream fos = openFileOutput("notice.txt", Context.MODE_WORLD_WRITEABLE);
String str = Integer.toString(1);
fos.write(str.getBytes());
fos.close();
} catch (IOException e) {}
}
public void stop() {
if(task != null){
task.cancel(); //타이머task를 timer 큐에서 지워버린다
task=null;
}
timer.cancel(); //스케쥴task과 타이머를 취소한다.
timer.purge(); //task큐의 모든 task를 제거한다.
timer=null;
try {
FileOutputStream fos = openFileOutput("notice.txt", Context.MODE_WORLD_WRITEABLE);
String str = Integer.toString(0);
fos.write(str.getBytes());
fos.close();
} catch (IOException e) {}
}