PHP

[안드로이드] BroadcastReceiver상속 클래스의 리부팅 해결 방법

미스털이 사용자 2025. 3. 4. 17:16
반응형

핸드폰을 껐다가 키는 경우가 종종 있다.

(업데이트 이슈가 대표적)

 

이 리부팅을 할 때 어플의 알람데이터가 없어진다. 이 데이터를 다시 복구하기 위해선 BroadcastReceiver를 상속하는 클래스의 onRecieve메소드를 다음과 같이 변경해줘야 한다.

 

(처음에 리부팅 상황이 아닌 경우와 리부팅 상황인 경우를 합쳐서 쓰다보니 아래의 createNotiChannelHigh메소드와 notifyFullScreen메소드를 리부팅할 때마다 호출하는 문제가 생겼었다. 이 메소드를 조건을 붙여 분리시켜야 한다.)

 

@Override
public void onReceive(Context context, Intent intent) {
    //createNotificationChannel(context); //그냥 메세지 위한 채널 생성


    //notifyNotification(context); //그냥 메세지를 위한 채널 생성
    //notifyFullScreen(context); //풀스크린 알람
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
        // 알람을 다시 설정하는 로직을 여기에 추가합니다.
        try {
            bookDB_at_booting = new BookDatabase(context);
            alarmStorage_at_booting = AlarmStorage.getInstance(context);
            getAlarmsFromDB(context);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    } else {
        //리부트 상황이 아닐 경우
        if (intent != null) {
            if (context != null) {
                createNotiChannelHigh(context); //풀스크린을 위한 채널 중요 설정 및 생성
                notifyFullScreen(context);
            }
        }
    }
}

 

위 처럼 해줘야 문제없이 쓸 수 있다.

 

반응형