Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

android sending sms and getting more and more send and delivery notification

I have a method to send sms. This method registers two pending intents to receive sending and delivery reports.

I send the first message and I receive its sending and delivery reports and then send another message. While receiving reports for second message, I get reports of first message and then second message. If I send another message, I get reports of first and second messages and then for the third message.

What's the problem?

Source:

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

Upvotes: 1

Views: 3504

Answers (3)

Ali Behzadian Nejad
Ali Behzadian Nejad

Reputation: 9044

problem is solved by adding this line at the end of OnReceive() methods:

 MyActivity.this.unregisterReceiver(this);

Closer look:

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

After receiving broadcast and processing it, we no longer need this broadcast receiver, so we unregister that from our Activity.

Upvotes: 2

Manikandan
Manikandan

Reputation: 1519

use this code to send and receive the sent notification

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

Upvotes: 2

user387184
user387184

Reputation: 11053

Did you put this in your 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;
        }
    }

where bRSMS_has_been_sent and brSMS_has_beed_delivered are my receivers

make sure you unregister the receivers, otherwise you have several hanging around.

If you like to see yourself, just use a static variable and increase it everytime you call your sending method and the do a log-output in the receiver. This way youcan see which receiver actually gets fired. You should see several receivers getting fired if you register all of them and never unregister any of them.

Upvotes: 0

Related Questions