Android 如何监控每个已发送短信的状态?

Android 如何监控每个已发送短信的状态?,android,sms,Android,Sms,我开发了向多个人发送短信的代码。但目前的问题是,我不知道哪个人收到了消息,哪个人发送失败了。我想知道每个发送短信的状态和接收者的电话号码,可以吗?这是您要查找的代码片段 //---sends an SMS message to another device--- private void sendSMS(String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED

我开发了向多个人发送短信的代码。但目前的问题是,我不知道哪个人收到了消息,哪个人发送失败了。我想知道每个发送短信的状态和接收者的电话号码,可以吗?

这是您要查找的代码片段

//---sends an SMS message to another device---

private void sendSMS(String phoneNumber, String message)
{        
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
        new Intent(DELIVERED), 0);

    //---when the SMS has been sent---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(getBaseContext(), "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
}

我也会推荐这个网站。。。如果您同时在一个循环中发送多条短信,您如何确定广播是为哪条短信而发送的?唯一标识符是什么?@MathiasLin你有什么解决方案吗?@MathiasLin,你可以在广播意图的意图附加中识别短信:意图意图=新意图(已发送);intent.putExtra(键、标识符);。您将在广播接收器(上面称为arg1)接收的意图中找到该数据。pendingent sentPI=pendingent.getBroadcast(此,请求代码,新意图(已发送),0);PendingEvent deliveredPI=PendingEvent.getBroadcast(此,请求代码,新意图(已交付),0);注意:为每条短信输入不同的请求代码。我在我的应用程序中成功实现了这一点。。请查收-