v0.3.2: new BootReceiver.kt
This commit is contained in:
1 parent
ea79599c08
commit
ad4cdef4cb
1 file changed
+48
@@ -0,0 +1,48 @@
|
||||
package com.hotdeal.alarm.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.hotdeal.alarm.data.local.preferences.AppSettings
|
||||
import com.hotdeal.alarm.worker.WorkerScheduler
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* 부팅 완료 시 백그라운드 폴링을 자동으로 재등록하는 리시버
|
||||
*/
|
||||
@AndroidEntryPoint
|
||||
class BootReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject
|
||||
lateinit var workerScheduler: WorkerScheduler
|
||||
|
||||
@Inject
|
||||
lateinit var appSettings: AppSettings
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == Intent.ACTION_BOOT_COMPLETED ||
|
||||
intent.action == Intent.ACTION_MY_PACKAGE_REPLACED
|
||||
) {
|
||||
Log.d("BootReceiver", "부팅/업데이트 감지 → 백그라운드 폴링 재등록")
|
||||
scope.launch {
|
||||
try {
|
||||
val interval = appSettings.pollingInterval.first()
|
||||
workerScheduler.schedulePeriodicPolling(interval.toLong())
|
||||
Log.d("BootReceiver", "폴링 재등록 완료: ${interval}분 간격")
|
||||
} catch (e: Exception) {
|
||||
Log.e("BootReceiver", "폴링 재등록 실패: ${e.message}")
|
||||
workerScheduler.schedulePeriodicPolling(WorkerScheduler.DEFAULT_INTERVAL_MINUTES)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user