检查短信是否实际发送

问题描述 投票:0回答:2

在我的短信应用程序中,我使用

SmsManager
发送短信。之后我想显示一个 Toast 说
"Message sent"
"Message not sent"
。如何检查消息是否确实已发送?可能是没有连接的问题?或者没有SIM卡?我怎样才能检测到这些?

android sms
2个回答
0
投票

我尝试了这个解决方案,它对我有用:

import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telephony.SmsManager
import android.telephony.SmsMessage
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val phoneNumber = "Your friend's phone number"
        val message = "Your SMS message"
        sendSMS(phoneNumber, message)
    }

    private fun sendSMS(phoneNumber: String, message: String) {
        val sentIntent = PendingIntent.getBroadcast(this, 0, Intent("SMS_SENT"), PendingIntent.FLAG_IMMUTABLE)
        val deliveredIntent = PendingIntent.getBroadcast(this, 0, Intent("SMS_DELIVERED"), PendingIntent.FLAG_IMMUTABLE)

        val smsManager = SmsManager.getDefault()

        smsManager.sendTextMessage(phoneNumber, null, message, sentIntent, deliveredIntent)
    }

    override fun onResume() {
        super.onResume()
        registerReceiver(sentReceiver, IntentFilter("SMS_SENT"))
        registerReceiver(deliveredReceiver, IntentFilter("SMS_DELIVERED"))
    }

    override fun onPause() {
        super.onPause()
        unregisterReceiver(sentReceiver)
        unregisterReceiver(deliveredReceiver)
    }

    private val sentReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (resultCode) {
                AppCompatActivity.RESULT_OK -> {
                    // SMS sent successfully
                    // You can update UI or show a toast here
                }
                SmsManager.RESULT_ERROR_GENERIC_FAILURE -> {
                    // Failed to send SMS
                    // You can update UI or show a toast here

                }
                SmsManager.RESULT_ERROR_NO_SERVICE -> {
                    // No service available
                    // You can update UI or show a toast here
                }
                SmsManager.RESULT_ERROR_NULL_PDU -> {
                    // Null PDU
                    // You can update UI or show a toast here
                }
                SmsManager.RESULT_ERROR_RADIO_OFF -> {
                    // Radio off
                    // You can update UI or show a toast here
                }
            }
        }
    }

    private val deliveredReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            when (resultCode) {
                AppCompatActivity.RESULT_OK -> {
                    // SMS delivered successfully
                    // You can update UI or show a toast here
                }
                AppCompatActivity.RESULT_CANCELED -> {
                    // SMS not delivered
                    // You can update UI or show a toast here
                }
            }
        }
    }
}

如果您收到此错误:

RESULT_ERROR_GENERIC_FAILURE 
,我尝试了此解决方案:merovingen 的解决方案


-6
投票

尝试下面的代码片段。这里的异常表示所有可能的失败情况

片段

try {
      SmsManager smsManager = SmsManager.getDefault();
      smsManager.sendTextMessage(number, null, sms, null, null);
      Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
 } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "SMS failed, please try again later!",
               Toast.LENGTH_LONG).show();
         e.printStackTrace();
}

查看示例此处

© www.soinside.com 2019 - 2024. All rights reserved.