1

我有一种发送短信的方法。此方法注册两个待处理的意图来接收发送和传递报告。

我发送第一条消息并收到它的发送和传递报告,然后发送另一条消息。在接收第二条消息的报告时,我收到第一条消息的报告,然后是第二条消息。如果我发送另一条消息,我会收到第一条和第二条消息的报告,然后是第三条消息。

有什么问题?

来源:

public void sendSms(String phoneNumber, String cmd ...) {
    sentPI      = PendingIntent.getBroadcast(this, sendSmsCounter++, new Intent(SENT),      PendingIntent.FLAG_CANCEL_CURRENT);
    deliveredPI = PendingIntent.getBroadcast(this, deliverCounter++, new Intent(DELIVERED), PendingIntent.FLAG_CANCEL_CURRENT);

    sendingBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    displayMessage("Sms Sent");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    displayMessage("Sending SMS error: Generic failure");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    displayMessage("Sending SMS error: No service");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    displayMessage("Sending SMS error: Null PDU");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    displayMessage("Sending SMS error: Radio off");
                    break;
            }               
        }
    };


    this.registerReceiver(sendingBroadcastReceiver, new IntentFilter(SENT));

    deliveryBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    displayMessage("SMS Delivered");
                    break;
                case Activity.RESULT_CANCELED:
                    displayMessage("SMS delivery error: SMS Canceled");
                    break;                        
            }               
        }
    };

    this.registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, cmd, sentPI, deliveredPI);        
4

3 回答 3

2

使用此代码发送和接收发送的通知

send.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            String phoneNo = "Phone number";
            String message = "Content of the msg";
            if (phoneNo.length() > 0 && message.length() > 0) {
                TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                int simState = telMgr.getSimState();
                switch (simState) {
                case TelephonyManager.SIM_STATE_ABSENT:
                    displayAlert();
                    break;
                case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                    // do something
                    break;
                case TelephonyManager.SIM_STATE_READY:
                    // do something
                    sendSMS(phoneNo, message);
                    break;
                case TelephonyManager.SIM_STATE_UNKNOWN:
                    // do something
                    break;
                }

            } else {
                Toast.makeText(getBaseContext(),
                        "Please enter both phone number and message.",
                        Toast.LENGTH_SHORT).show();
            }

        }



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

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

    PendingIntent deliveredPI = PendingIntent.getBroadcast(YourActivity.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(YourActivity.this, "SMS sent",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(YourActivity.this, "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(YourActivity.this, "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(YourActivity.this, "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(YourActivity.this, "SMS delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(YourActivity.this, "SMS not delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    Intent smsintent = new Intent(YourActivity.this, SecondActivity.Activity);
    startActivity(smsintent);
}
于 2012-04-30T13:49:18.990 回答
2

通过在 OnReceive() 方法的末尾添加这一行来解决问题:

 MyActivity.this.unregisterReceiver(this);

仔细看看:

sendingBroadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (getResultCode())
        {
            case Activity.RESULT_OK:
                displayMessage("Sms Sent");
                break;
            // ...    
        }
        MyActivity.this.unregisterReceiver(this);               
    }
};

在接收到广播并处理之后,我们不再需要这个广播接收器,所以我们从我们的 Activity 中取消注册它。

于 2012-05-04T17:01:08.040 回答
0

你把它放在你的onPause中了吗?

@Override
    protected void onPause() {
        super.onPause();
        if (bRSMS_has_been_sent != null){
            unregisterReceiver(bRSMS_has_been_sent);
            bRSMS_has_been_sent = null;
        }
        if (brSMS_has_beed_delivered != null){
            unregisterReceiver(brSMS_has_beed_delivered);
            brSMS_has_beed_delivered = null;
        }
    }

bRSMS_has_been_sent我的brSMS_has_beed_delivered 接收者在哪里?

确保你取消注册接收器,否则你有几个闲逛。

如果您想看到自己,只需使用静态变量并在每次调用发送方法时增加它并在接收器中执行日志输出。通过这种方式,您可以看到实际触发了哪个接收器。如果您注册所有接收者并且从不取消注册其中任何一个,您应该会看到多个接收者被解雇。

于 2012-04-30T13:48:49.990 回答