| | |
| | | import android.content.Intent |
| | | import android.os.Build |
| | | import androidx.core.app.NotificationCompat |
| | | import androidx.work.* |
| | | import androidx.work.CoroutineWorker |
| | | import androidx.work.WorkerParameters |
| | | import com.example.firstapp.MainActivity |
| | |
| | | import kotlinx.coroutines.Dispatchers |
| | | import kotlinx.coroutines.flow.first |
| | | import kotlinx.coroutines.withContext |
| | | import java.util.Calendar |
| | | import java.util.concurrent.TimeUnit |
| | | |
| | | /** |
| | |
| | | override suspend fun doWork(): Result = withContext(Dispatchers.IO) { |
| | | android.util.Log.d("ReminderWorker", "doWork 开始执行") |
| | | try { |
| | | // 强制发送一条测试通知(确认Worker是否执行) |
| | | // sendTestNotification() |
| | | android.util.Log.d("ReminderWorker", "发送测试通知确认Worker已执行") |
| | | |
| | | // 获取所有提醒设置 |
| | | val reminderList = reminderRepository.getAllReminders().first() |
| | | android.util.Log.d("ReminderWorker", "获取到 ${reminderList.size} 条提醒设置") |
| | | |
| | | if (reminderList.isEmpty()) { |
| | | // 测试用:如果没有数据,也发送一条测试通知 |
| | | sendTestNotification() |
| | | android.util.Log.d("ReminderWorker", "数据为空,发送了测试通知") |
| | | } |
| | | |
| | | for (reminder in reminderList) { |
| | | try { |
| | | android.util.Log.d("ReminderWorker", "处理提醒: ${reminder.categoryName}") |
| | | checkCategoryContent(reminder.categoryId, reminder.categoryName, reminder.notificationMethod) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | android.util.Log.e("ReminderWorker", "处理提醒失败: ${e.message}") |
| | | if (reminderList.isNotEmpty()) { |
| | | for (reminder in reminderList) { |
| | | try { |
| | | android.util.Log.d("ReminderWorker", "处理提醒: ${reminder.categoryName}") |
| | | checkCategoryContent(reminder.categoryId, reminder.categoryName, reminder.notificationMethod) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | android.util.Log.e("ReminderWorker", "处理提醒失败: ${e.message}") |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | companion object { |
| | | private const val CHANNEL_ID = "reminder_channel" |
| | | |
| | | // 定时任务执行频率 - 设置为每天运行一次 |
| | | val REPEAT_INTERVAL = 24L // 每24小时运行一次 |
| | | val REPEAT_INTERVAL_TIME_UNIT = TimeUnit.HOURS |
| | | |
| | | // 测试用的频率配置,已注释掉 |
| | | // val REPEAT_INTERVAL = 2L |
| | | // val REPEAT_INTERVAL_TIME_UNIT = TimeUnit.MINUTES |
| | | // 定时任务执行频率 - 可选不同配置 |
| | | val REPEAT_INTERVAL = 15L // 设置为每15分钟运行一次 |
| | | val REPEAT_INTERVAL_TIME_UNIT = TimeUnit.MINUTES |
| | | |
| | | // 创建在指定时间运行的定时任务 |
| | | fun setupScheduledWorker(context: Context, hour: Int, minute: Int) { |
| | | val calendar = Calendar.getInstance() |
| | | val now = Calendar.getInstance() |
| | | |
| | | calendar.set(Calendar.HOUR_OF_DAY, hour) |
| | | calendar.set(Calendar.MINUTE, minute) |
| | | calendar.set(Calendar.SECOND, 0) |
| | | |
| | | // 如果设定时间已经过了,则设置为明天这个时间 |
| | | if (calendar.timeInMillis < now.timeInMillis) { |
| | | calendar.add(Calendar.DAY_OF_MONTH, 1) |
| | | } |
| | | |
| | | // 计算初始延迟 |
| | | val initialDelay = calendar.timeInMillis - now.timeInMillis |
| | | |
| | | // 创建周期性工作请求(每天同一时间) |
| | | val dailyWorkRequest = PeriodicWorkRequestBuilder<ReminderWorker>( |
| | | 24, TimeUnit.HOURS |
| | | ) |
| | | .setInitialDelay(initialDelay, TimeUnit.MILLISECONDS) |
| | | .build() |
| | | |
| | | // 使用 REPLACE 策略确保新配置生效 |
| | | WorkManager.getInstance(context).enqueueUniquePeriodicWork( |
| | | "daily_reminder_${hour}_${minute}", |
| | | ExistingPeriodicWorkPolicy.REPLACE, |
| | | dailyWorkRequest |
| | | ) |
| | | } |
| | | } |
| | | } |