| | |
| | | import com.example.firstapp.database.repository.CodeRepository |
| | | import com.example.firstapp.database.repository.KeywordRepository |
| | | import com.example.firstapp.database.repository.MsgRepository |
| | | import com.example.firstapp.database.repository.ReminderRepository |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.receiver.CactusReceiver |
| | | import com.example.firstapp.service.ReminderWorker |
| | |
| | | val msgRepository by lazy { MsgRepository(database.msgDao()) } |
| | | val codeRepository by lazy { CodeRepository(database.codeDao()) } |
| | | val keywordRepository by lazy { KeywordRepository(RetrofitClient.apiService,database.keywordDao()) } |
| | | val reminderRepository by lazy { ReminderRepository(this) } |
| | | |
| | | companion object { |
| | | const val TAG: String = "SmsForwarder" |
| | |
| | | * 设置定时提醒Worker |
| | | * 配置为每天运行一次检查是否有新的提醒内容 |
| | | */ |
| | | private fun setupReminderWorker() { |
| | | fun setupReminderWorker() { |
| | | Log.d(TAG, "设置定时提醒Worker") |
| | | |
| | | // 方式1:使用周期性执行 |
| | | val constraints = Constraints.Builder() |
| | | .setRequiredNetworkType(NetworkType.CONNECTED) |
| | | .build() |
| | | |
| | | // 创建周期性工作请求 |
| | | val reminderWorkRequest = PeriodicWorkRequestBuilder<ReminderWorker>( |
| | | ReminderWorker.REPEAT_INTERVAL, |
| | | ReminderWorker.REPEAT_INTERVAL_TIME_UNIT |
| | | ) |
| | | .setConstraints(constraints) |
| | | .addTag("reminder_worker") |
| | | .build() |
| | | |
| | | // 使用 REPLACE 策略确保新配置生效 |
| | | WorkManager.getInstance(this).enqueueUniquePeriodicWork( |
| | | "reminder_work", |
| | | ExistingPeriodicWorkPolicy.REPLACE, |
| | | reminderWorkRequest |
| | | ) |
| | | |
| | | // 方式2:设置在特定时间执行(例如每天上午9点和下午18点) |
| | | // 仅在特定时间执行(每天指定时间) |
| | | // 可根据需要设置多个不同时间点的提醒 |
| | | ReminderWorker.setupScheduledWorker(this, 9, 0) // 上午9:00 |
| | | ReminderWorker.setupScheduledWorker(this, 13, 50) // 下午18:00 |
| | | ReminderWorker.setupScheduledWorker(this, 13, 50) // 下午13:50 |
| | | |
| | | // 注意:不再立即执行一次提醒检查,避免重复提醒 |
| | | // 如果需要立即检查,可以设置一个延迟,例如5分钟后执行 |
| | | val delayedWorkRequest = OneTimeWorkRequestBuilder<ReminderWorker>() |
| | | .setInitialDelay(5, TimeUnit.MINUTES) // 延迟5分钟执行 |
| | | .build() |
| | | WorkManager.getInstance(this).enqueue(delayedWorkRequest) |
| | | // 不再使用周期性轮询和立即执行的方式 |
| | | } |
| | | |
| | | } |