본문 바로가기

안드로이드

버전별로 알람처리 방법

반응형


버전별로 알람처리 방법




int id = 999; //알람아이디값 생성

Intent intent = new Intent(AlarmAddActivity.this, AlarmRecever.class);

//알람이 지정되면 받는 브로드 캐스트 리시

intent.putExtra("idx", id);




PendingIntent sender = PendingIntent.getBroadcast

(AlarmAddActivity.this, id, intent,PendingIntent.FLAG_UPDATE_CURRENT);/

/지연된 인텐드, 알람이 울리면 인텐드 값을 전달한다.


String alamrTime = "2017-11-20 08:00:00";

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(alamrTime); // 알람 등록할 시간을 지정해준다


long aTime = System.currentTimeMillis();
long bTime = date.getTime();

//하루의 시간을 나타냄
long interval = 1000 * 60 * 60 * 24;

if(aTime>bTime){ //알람 설정시간이 현재시간 보다 적으면 24후에 알람 설정
bTime += interval;
}

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

if(Build.VERSION.SDK_INT>=23){ //버전별로 알람처리
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, bTime, sender);
}else{
if(Build.VERSION.SDK_INT>=19){
am.setExact(AlarmManager.RTC_WAKEUP, bTime, sender);
}else{
am.set(AlarmManager.RTC_WAKEUP, bTime, sender);
}
}

//휴대폰이 재 부팅되었을때 처리하려면 알람 시간및 내용을 내장 db에 저장후

BOOT_COMPLETED시 다시 불러와서 알람을 재등록한다.

//알람 반복은 AlarmRecever에서 알림 울렸을때 알람 리셋후 재 등록하면 된다.


반응형