【问题标题】:How to know SMS is correctly sent in Android?如何知道短信在 Android 中是否正确发送?
【发布时间】:2021-05-14 09:41:33
【问题描述】:

我正在制作 SMS Manager 应用程序。 这是我的代码。

接收方代码:

private val receiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        val id = intent.getIntExtra("id", 0)
        if (resultCode == Activity.RESULT_OK) {
            Log.d("SMS", "Success to sent SMS")
        } else {
            Log.e("SMS", "Failed to send SMS")
        }
    }
}

发送短信方式:

private fun sendMessage(phone: String, message: String) {
    try {
        Log.d("SMS", "Send SMS")
        val intent = Intent(SENT)
        val sentIntent = PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        smsManager.sendTextMessage(phone, null, message, sentIntent, null)
    } catch (ex: Exception) {
        Log.e("Error", "error", ex)
    }
}

当我向正确的号码发送消息时,接收者可以收到“成功”事件。很好!
但是当我向“123123123”等随机数发送消息时,接收方也会收到“成功”事件。这很糟糕!
所以我检查了我的手机,但默认消息应用程序中出现了失败的消息。

所以我的问题是:
为什么在我的代码的 sendIntent 中广播 success event
我该如何解决这个问题?

请任何人帮助我。
谢谢。

附言。 我已经看过以下网址。但是还是没有答案。

sms is always display "sms sent" even when using random cellphone number without delivery reports - android

Android sms is always sent same contact

How to test sms sending failure in emulator

【问题讨论】:

    标签: android kotlin broadcastreceiver android-pendingintent android-sms


    【解决方案1】:

    SENT 状态仅反映 SMS 从手机到陆侧 SMSC(服务器)的传输。当您获得成功时,这意味着消息已成功从手机传输到服务器。它与将消息实际传递给收件人没有任何关系。

    如果您想了解交货状态,您需要创建一个额外的PendingIntent 并将其传递给SmsManager

        val intent = Intent(SENT)
        val sentIntent = PendingIntent.getBroadcast(activity, 0, intent,
                                PendingIntent.FLAG_ONE_SHOT)
        val intent2 = Intent(DELIVERY)
        PendingIntent.getBroadcast(activity, 0, intent2,
                                PendingIntent.FLAG_ONE_SHOT)
        smsManager.sendTextMessage(phone, null, message, sentIntent, deliveryIntent)
    

    然后您的BroadcastReceiver 可以捕获Intent 的传递并确定消息是否已成功传递。

    【讨论】:

    • 我用你的方法试过了。但任何事件都没有传递给 deliveryIntent。
    • 请编辑您的问题并显示您的代码。请同时出示您的清单。
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 2017-08-12
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 2011-06-04
    相关资源
    最近更新 更多