0

I want to send sms through sms manager (android). I also want to know the condition of that sms (sent or not, is delivered etc). For this I use this code

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

From Here Android sms manager not sending sms

It works fine for me. But I am facing some problem. As example, at first I send a message to 1234. it sent and delivered. Its OK. Second time I send another number 1235. It also sent and delivered. Its also OK, but problem is it also showing previous message condition also (sent, delivered or not). But I want to show one message condition just for one time. How can I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Xplosive
  • 711
  • 3
  • 14
  • 26

1 Answers1

2

first apologies for my english is basic. the problem with this code is that the "registerReceiver(new BroadcastReceiver()" is created every time that the function is called, then i solve this problem created one function that initialize the two "BroadcastReceiver", this function is called on "protected void onCreate". the next code explain better:

@Override
protected void onCreate(Bundle icicle){
    super.onCreate(icicle);
    iniciar();
}

@Override
protected void onResume(){
    super.onResume();
}

@Override
protected void onStop(){
    super.onStop();
}

@Override 
protected void onPause(){
    super.onPause();
}

 PendingIntent sentPI;
 PendingIntent deliveredPI;

 public void iniciar(){
     String SENT = "SMS_SENT";
     String DELIVERED = "SMS_DELIVERED";

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

      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) {
                Toast.makeText(MainActivity.this, "se recibe respuesta!", Toast.LENGTH_SHORT).show();
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS sent!/Mensaje enviado!",Toast.LENGTH_SHORT).show();

                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure/posiblemente no tienes saldo",Toast.LENGTH_SHORT).show();

                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service/no hay señal",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/SMS entregado",Toast.LENGTH_SHORT).show();

                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered/SMS no entregado", Toast.LENGTH_SHORT).show();

                    break;
                }
            }
        }, new IntentFilter(DELIVERED));


 }

 public void sendSMS(String phoneNumber, String message) {
        showMessage("Sending../enviando..");   
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);   
 }
Rodrigo porras
  • 196
  • 2
  • 8