cloudroam
2025-02-21 2167ea58d1c297b0536d5cab6517707f1892b95f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.example.firstapp.receiver
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Telephony
import android.telephony.SmsMessage
import android.util.Log
import androidx.annotation.RequiresApi
import com.example.firstapp.core.Core
import com.example.firstapp.database.entity.Code
import com.example.firstapp.database.entity.Msg
import com.example.firstapp.database.repository.KeywordRepository
import com.example.firstapp.entity.Rule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
 
 
class SmsReceiver : BroadcastReceiver() {
 
 
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {
 
        // 检查广播的 Action 是否为短信接收
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION == intent.action) {
            // 获取短信内容
            val bundle: Bundle? = intent.extras
            bundle?.let {
                val pdus = it.get("pdus") as Array<*>
                val messages = arrayOfNulls<SmsMessage>(pdus.size)
                val messageBody = StringBuilder()
 
                for (i in pdus.indices) {
                    messages[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
                    messageBody.append(messages[i]?.messageBody)
                }
 
                // 输出短信内容到控制台
                Log.d("SmsReceiver", "Received SMS: ${messageBody.toString()}")
                val msg = Msg(0, "1111", "111111", messageBody.toString(),1, "111", 1, 1)
                val msgId = Core.msg.insert(msg)
                Log.d("SmsReceiver", "Received SMS msgId: ${msgId}")
 
                // 这里我要写个数组,并创建个对象存放一些内容,如这个对象的属性有匹配内容,正则表达式,并循环遍历
                val ruleList = listOf(
                    Rule("快递","京东","\\d{6}"),
                    Rule("快递","菜鸟驿站","\\d{1,2}-\\d{1,2}-\\d{4}")
                )
 
                CoroutineScope(Dispatchers.IO).launch {
                    Log.d("SmsReceiver", "CoroutineScope started")
                    // 获取最新的关键词配置
                    val keywords = Core.keyword.getKeywords()
                    Log.d("keywords", keywords.toString())
                    println(keywords)
                    // 保存匹配的短信
                    //saveMessage(content)
                }
 
 
                // kotlin 怎么创建一个类
                for (rule in ruleList) {
                    val code = rule.extractCodeFromMessage(messageBody.toString())
 
                    if (code!==null) {
                        Log.d("SmsReceiver", "Received SMS code: ${code}")
 
 
                        // 获取当前时间
                        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", "发送数据更新广播")
                    }else{
                        Log.d("SmsReceiver", "Received SMS code: 没有匹配到内容")
                    }
                }
 
            }
        }
    }
}