개발은 하는건가..

[Android] AlarmManager 의 RTC_WAKEUP 을 통한 작업 수행. 본문

Java, Android

[Android] AlarmManager 의 RTC_WAKEUP 을 통한 작업 수행.

수동애비 2022. 10. 26. 10:55
반응형

 

설정 시간 이후 깨어나서 필요한 동작을 수행하도록 한다.
요즘 추세는  WAKEUP 을 하지 않고 WorkManager 를 이용하라고는 하지만 여전히 잘 동작하고 옛날 방식이 더 편한 감도 있다.

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent newIntent = new Intent(Constants.DELAYED_ALARM_WAKE_UP);
newIntent.setPackage(getPackageName());
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

timeDiff += System.currentTimeMillis();
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeDiff, pendingIntent);

timeDiff 시간 이후  Constants.DELAYED_ALARM_WAKE_UP  action 을 수신할 수 있는 BroadcastReceiver 만 생성하여 사용하면 된다.

BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
		
        if (action.equals(Constants.DELAYED_ALARM_WAKE_UP)) {
         	// TODO. 동작 처리.
         }
    }
}

예전에는 PendingIntent 생성 시 플래그를 0 으로 설정해도 문제가 없었는데 요즘은 PendingIntent.FLAG_IMMUTABLE 플래그를 설정해야 마켓 등록 시 퇴짜 맞지 않는다.

 

Comments