tj
2025-03-20 90b7acecef96a7b4927301847efbe11a1f0336c2
app/src/main/java/com/example/firstapp/receiver/SmsReceiver.kt
@@ -13,6 +13,7 @@
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.database.service.RetrofitClient
import com.example.firstapp.entity.Rule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@@ -27,12 +28,28 @@
class SmsReceiver : BroadcastReceiver() {
    // 安全防护关键词数组
    private var securityKeywordsList = emptyList<String>()
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onReceive(context: Context, intent: Intent) {
        // 检查广播的 Action 是否为短信接收
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION == intent.action) {
            CoroutineScope(Dispatchers.IO).launch {
                try {
                    val response = RetrofitClient.apiService.getSecurityList();
                    // 这里需要将response.data存放到变量中
                    if(response.code===200){
                        securityKeywordsList = response.data.map { it.keyword }
                        Log.d("SmsReceiver", "securityKeywordsList: $securityKeywordsList")
                    }
                }catch (e: Exception){
                    Log.d("SmsReceiver", "Error: ${e.message}")
                }
            }
            // 获取短信内容
            val bundle: Bundle? = intent.extras
            bundle?.let {
@@ -44,9 +61,19 @@
                    messages[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray)
                    messageBody.append(messages[i]?.messageBody)
                }
                // 输出短信内容到控制台
                val msg = Msg(0, "1111", "111111", messageBody.toString(),1, "111", 1, 1)
                val msgId = Core.msg.insert(msg)
                // 这里需要查看消息是否含有securityKeywordsList这个列表中的关键词,如果含有,则不进行下一步操作
                if (securityKeywordsList.any { it in messageBody.toString() }) {
                    Log.d("SmsReceiver", "含有禁用关键词,${it}")
                    return
                }
                Log.d("SmsReceiver", "运行到正则匹配")
                // 这里我要写个数组,并创建个对象存放一些内容,如这个对象的属性有匹配内容,正则表达式,并循环遍历
                val ruleList = mutableListOf(
                    Rule("快递","京东","\\d{6}"),