From cb99bd7dad1b305a434c5c6c99ca65e782eb0f34 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期五, 11 四月 2025 17:32:12 +0800 Subject: [PATCH] add: 消息提醒 --- app/src/main/java/com/example/firstapp/service/ReminderWorker.kt | 70 +++++++++++++++++++++++++---------- 1 files changed, 50 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/service/ReminderWorker.kt b/app/src/main/java/com/example/firstapp/service/ReminderWorker.kt index 07b811a..5e9d3fd 100644 --- a/app/src/main/java/com/example/firstapp/service/ReminderWorker.kt +++ b/app/src/main/java/com/example/firstapp/service/ReminderWorker.kt @@ -7,6 +7,7 @@ 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 @@ -21,6 +22,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.first import kotlinx.coroutines.withContext +import java.util.Calendar import java.util.concurrent.TimeUnit /** @@ -38,23 +40,23 @@ 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}") + } } } @@ -232,12 +234,40 @@ 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 + ) + } } } \ No newline at end of file -- Gitblit v1.9.3