| | |
| | | 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.entity.Rule |
| | | import com.example.firstapp.database.service.RetrofitClient |
| | | import com.example.firstapp.database.service.RetrofitModelClient |
| | | 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.util.Date |
| | | import java.util.Locale |
| | | import com.example.firstapp.utils.CodeUtils |
| | | |
| | | |
| | | 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) { |
| | | // 获取短信内容 |
| | | val bundle: Bundle? = intent.extras |
| | | bundle?.let { |
| | | val pdus = it.get("pdus") as Array<*> |
| | | val messages = arrayOfNulls<SmsMessage>(pdus.size) |
| | | val messageBody = StringBuilder() |
| | | |
| | | // 获取短信时间戳(使用第一条短信的时间戳) |
| | | var timestamp: Long = 0 |
| | | |
| | | 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}") |
| | | ) |
| | | |
| | | // kotlin 怎么创建一个类 |
| | | for (rule in ruleList) { |
| | | val code = rule.extractCodeFromMessage(messageBody.toString()) |
| | | |
| | | if (code!==null) { |
| | | Log.d("SmsReceiver", "Received SMS code: ${code}") |
| | | // 封装成一个Code对象,并保存在数据库中 |
| | | val code = Code(0, rule.type,1, rule.content,1, 1, msgId, code) |
| | | Core.code.insert(code) |
| | | // 发送广播通知数据已更新 |
| | | val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | context.sendBroadcast(updateIntent) |
| | | }else{ |
| | | Log.d("SmsReceiver", "Received SMS code: 没有匹配到内容") |
| | | if (i == 0) { |
| | | timestamp = messages[i]?.timestampMillis ?: System.currentTimeMillis() |
| | | } |
| | | } |
| | | |
| | | // 保存原始短信 |
| | | 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", "运行到正则匹配") |
| | | |
| | | // 调用API处理短信 |
| | | CoroutineScope(Dispatchers.IO).launch { |
| | | try { |
| | | val response = |
| | | RetrofitModelClient.modelService.processSms(mapOf("content" to messageBody.toString())) |
| | | |
| | | if (response.status == "success") { |
| | | // 获取当前时间 |
| | | val currentTime = LocalDateTime.now() |
| | | val date = |
| | | Date.from(currentTime.atZone(ZoneId.systemDefault()).toInstant()) |
| | | val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) |
| | | val createtime = sdf.format(date) |
| | | |
| | | // 根据不同类型处理数据 |
| | | when (response.data.category) { |
| | | "快递" -> { |
| | | val code = CodeUtils.createExpressCode( |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | post = response.data.details.post, |
| | | company = response.data.details.company, |
| | | pickupCode = response.data.details.pickupCode, |
| | | address = response.data.details.address, |
| | | time = response.data.details.time, |
| | | smsTimestamp = timestamp // 添加短信时间戳 |
| | | ) |
| | | CodeUtils.saveCode(code) |
| | | } |
| | | |
| | | "还款" -> { |
| | | val code = CodeUtils.createRepaymentCode( |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | type = response.data.details.type, |
| | | bank = response.data.details.bank, |
| | | amount = response.data.details.amount, |
| | | date = response.data.details.date, |
| | | address = response.data.details.address, |
| | | minAmount = response.data.details.min_amount, |
| | | number = response.data.details.number, |
| | | smsTimestamp = timestamp // 添加短信时间戳 |
| | | ) |
| | | CodeUtils.saveCode(code) |
| | | } |
| | | |
| | | "收入" -> { |
| | | val code = CodeUtils.createIncomeCode( |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | bank = response.data.details.bank, |
| | | amount = response.data.details.amount, |
| | | datetime = response.data.details.datetime, |
| | | address = response.data.details.address, |
| | | balance = response.data.details.balance, |
| | | smsTimestamp = timestamp // 添加短信时间戳 |
| | | ) |
| | | CodeUtils.saveCode(code) |
| | | } |
| | | |
| | | "航班" -> { |
| | | val code = CodeUtils.createFlightCode( |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | company = response.data.details.company, |
| | | start = response.data.details.start, |
| | | end = response.data.details.end, |
| | | seat = response.data.details.seat, |
| | | time = response.data.details.time, |
| | | address = response.data.details.address, |
| | | smsTimestamp = timestamp // 添加短信时间戳 |
| | | ) |
| | | CodeUtils.saveCode(code) |
| | | } |
| | | |
| | | "火车票" -> { |
| | | val code = CodeUtils.createTrainTicketCode( |
| | | msgId = msgId, |
| | | createTime = createtime, |
| | | company = response.data.details.company, |
| | | seat = response.data.details.seat, |
| | | time = response.data.details.time, |
| | | address = response.data.details.address, |
| | | trips = response.data.details.trips, |
| | | smsTimestamp = timestamp // 添加短信时间戳 |
| | | ) |
| | | CodeUtils.saveCode(code) |
| | | } |
| | | } |
| | | |
| | | // 发送广播通知数据已更新 |
| | | val updateIntent = Intent("com.example.firstapp.DATA_UPDATED") |
| | | context.sendBroadcast(updateIntent) |
| | | Log.d("SMS_DEBUG", "新短信已保存到数据库") |
| | | } |
| | | } catch (e: Exception) { |
| | | Log.e("SmsReceiver", "Error processing SMS", e) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |