1

在我的应用程序中,我使用 SMSManager 在后台向特定号码发送消息。我已经注册了接收者来验证我的消息的发送状态、传递状态。这工作正常。但是当我发送消息时,如果电话号码被关闭或SIM卡被移除并重新打开或SIM卡被重新插入,我的消息不会被发送。如何实现这个功能?

短信管理器代码

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

接收器:

smsSentReceiver = new BroadcastReceiver() {


            public void onReceive(Context arg0, Intent arg1) {

                switch(getResultCode())
                {

                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_LONG).show();
                break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Sms failed due to generic failure", Toast.LENGTH_LONG).show();
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "could not send the message due to unavailability of service.", Toast.LENGTH_LONG).show();
                    break;

                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_LONG).show();
                    break;

                }

            }
        };

            smsDeliveredReceiver = new BroadcastReceiver () {

            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;
                }

                }

            };

            registerReceiver(smsSentReceiver, new IntentFilter(SENT));
            registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED ));

    }
4

1 回答 1

0

您可以通过使用“Broadcst Reciver”来实现这一点。打开手机后,您将收到一个广播接收器,例如插入了新的 sim 卡,并尝试使用挂起的意图。

于 2012-12-28T09:22:17.407 回答