From 1097c45d8d6aa4b74a50e8d9a99dedab73f2bbad Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期五, 28 二月 2025 13:35:07 +0800
Subject: [PATCH] add: 优化首页自动同步历史短信,和实时刷新包裹列表

---
 app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt |   45 +++++++++++++++++++++++++--------------------
 1 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt b/app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt
index db9f14c..3ee32a5 100644
--- a/app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt
+++ b/app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt
@@ -17,8 +17,12 @@
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.launch
+import java.text.SimpleDateFormat
 import java.time.LocalDateTime
+import java.time.ZoneId
 import java.time.format.DateTimeFormatter
+import java.util.Date
+import java.util.Locale
 
 
 class SmsReceiver : BroadcastReceiver() {
@@ -68,30 +72,31 @@
                         )
                     }
                     Log.d("RuleList", ruleList.toString())
+
                     for (rule in ruleList) {
                         val code = rule.extractCodeFromMessage(messageBody.toString())
-
                         if (code!==null) {
-                            Log.d("SmsReceiver", "Received SMS code: ${code}")
-
-
-                            // 获取当前时间
+                            // 转换为 Date 对象
                             val currentTime = LocalDateTime.now()
-                            // 加2小时
-                            val futureTime = currentTime.plusHours(2)
-                            // 定义时间格式
-                            val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
-                            // 转换为字符串
-                            val overtime = futureTime.format(formatter)
-                            // 封装成一个Code对象,并保存在数据库中
-                            val code = Code(0, rule.type,1, rule.content,1, 1,  msgId, code, overtime,"中通")
-                            Core.code.insert(code)
-                            Log.d("SMS_DEBUG", "新短信已保存到数据库")
-                            // 发送广播通知数据已更新
-                            //"com.example.firstapp.DATA_UPDATED" 是一个自定义的广播 Action,相当于一个标识符或者说是一个频道名称。这个名称是我们自己定义的,通常使用应用的包名作为前缀,以避免与其他应用的广播冲突。
-                            val updateIntent = Intent("com.example.firstapp.DATA_UPDATED")
-                            context.sendBroadcast(updateIntent)
-                            Log.d("SMS_DEBUG", "发送数据更新广播")
+                            // 将 LocalDateTime 转换为 Date
+                            val date = Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant())
+                            // 如果需要格式化显示
+                            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
+                            val overtime = sdf.format(date)
+                            val existingCode = Core.code.queryByTypeAndCodeAndDate(rule.content, code, overtime)
+                            if (existingCode == null) {
+                                val code = Code(0, rule.type, 1, rule.content, 1, 1, msgId, code, overtime, "中通")
+                                Core.code.insert(code)
+                                Log.d("SMS_DEBUG", "新短信已保存到数据库")
+
+                                // 发送广播通知数据已更新
+                                //"com.example.firstapp.DATA_UPDATED" 是一个自定义的广播 Action,相当于一个标识符或者说是一个频道名称。这个名称是我们自己定义的,通常使用应用的包名作为前缀,以避免与其他应用的广播冲突。
+                                val updateIntent = Intent("com.example.firstapp.DATA_UPDATED")
+                                context.sendBroadcast(updateIntent)
+                                Log.d("SMS_DEBUG", "发送数据更新广播")
+                            }else{
+                                Log.d("SmsReceiver", "Received SMS code: 已存在相同记录,不保存")
+                            }
                         }else{
                             Log.d("SmsReceiver", "Received SMS code: 没有匹配到内容")
                         }

--
Gitblit v1.9.3