BroadcastReciever doesn't show SMS not Send or Not delivered

Posted by user1657111 on Stack Overflow See other posts from Stack Overflow or by user1657111
Published on 2012-09-08T18:34:31Z Indexed on 2012/09/08 21:38 UTC
Read the original article Hit count: 197

Filed under:
|

While using broadcastreciver for checking sms status, it shows the toast when sms is sent but shows nothing when sms is not sent or delivered (im testing it by putting an abrupt number). the code im using is the one ive seen the most on every site of checking sms delivery status. But my code is only showing the status when sms is sent successfully. Can any one get a hint of what am i doing wrong ? I hav this method in doInBackground() and so obviously im using AsyncTask. Thanks guys

public void send_SMS(String list, String msg, AtaskClass task) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();

    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 context, Intent arg1) {
            switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS sent", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show();
                    break;

                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "No service", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "Null PDU", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "Radio off", 
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    //---when the SMS has been delivered---
    registerReceiver(new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent arg1) {

              switch (getResultCode())
            {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, "SMS not delivered", 
                            Toast.LENGTH_SHORT).show();
                    break;                        
            }
        }
    }, new IntentFilter(DELIVERED));        

     StringTokenizer st = new StringTokenizer(list,",");    
        int count= st.countTokens();
        int i =1;
        count = 1;

            while(st.hasMoreElements())

            {

                //  PendingIntent pi = PendingIntent.getActivity(this,0,new Intent(this, SMS.class),0);

                    String tempMobileNumber = (String)st.nextElement();

                    //SmsManager sms = SmsManager.getDefault();

                    sms.sendTextMessage(tempMobileNumber, null, msg , sentPI, deliveredPI); 

                      Double cCom = ((double)i/count) * 100; 


                    int j = cCom.intValue();

              task.doProgress(j);
              i++;
              count ++;
               }

     // class ends
    }

© Stack Overflow or respective owner

Related posts about android

Related posts about sms