1

I am referring this article for sending an sms from Android.

I tried sending 10 sms to 10 different numbers with implementing loop. e.g. looping over sendSMS(phoneNo, message); But after sending 6 sms, sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); gives NullPointerExeption.

FYI, I am passing null in deliveredPI as I don't care about the delivery of sms. But it is very important to know if sms is really sent, and this can be retrieved by registering broadcast receiver.

I've registered a broadcast receiver, but it seems like whole process has no synchronization. We should send second sms only after we receive status of previous sms.

I am assuming that slowing down the speed of sending an sms will help me to remove NullPointerExeption. And waiting for status will be the best idea to keep time gap between sending another sms.

In short, I'd like to do -> send one sms -> wait for status -> update status in db -> send another sms.

Vikas
  • 24,082
  • 37
  • 117
  • 159

3 Answers3

2

I personally haven't tested the code I'm posting, but it was accepted as the answer for this other question so I assume it will work.

    SmsManager smsMan = new SmsManager.getDefault();
    ArrayList<String> contactList = new ArrayList();
    //add contacts to contactList with contactList.add(string)
    for (int i = 0; i <= contactList().size(); i++) {
    String SENT = contactList.get(i).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT, 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));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}

The above way, you will send a request to Android to send each message one after another. If you want to actually send the next SMS after Activity.RESULT_OK, then I would recommend still using the ArrayList approach, but instead of a for loop, you could have something like:

public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
    //add contacts to contactList with contactList.add(string)
    String SENT = contactList.get(position).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

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

            //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK
                        context.unregisterReceiver(this);
                        i++;
                        if (contactList.size()<i){
                            sendSms(i);
                        } else {
                            //You are done sending - Do what you want.
                        }
                        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));
    smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);

}

Again. I haven't tested it, but it should work. Let me know if you have any more questions or if I was unclear about anything.

Community
  • 1
  • 1
Reed
  • 14,703
  • 8
  • 66
  • 110
  • Code you provided is leading to recursion, I am worrying about its effect. However I'll implement it, and post here if any.. – Vikas Dec 12 '11 at 09:31
  • @Vikas I'm sorry. I had messed the code up in my initial post. I now have updated both the first block and second blocks so that the code is how it needs to be. I hope this helps more now. – Reed Dec 12 '11 at 18:21
  • Thanks & it's okay, I got your point in first post. I tried that code but similar issue., after sending 6 sms, my service crashes... I'm trying to create new project (removing extra codes), and trying to provide you a zip so that you can play with that.. – Vikas Dec 13 '11 at 04:23
0

I'm putting my comment as an answer because I can't actually comment. Jakar, your solution you provided will NOT work. You are not unregistering the broadcast receivers anywhere. Your code will raise an error.

bogdan
  • 338
  • 6
  • 10
0

I assume you found your answer already. But since the question still remains here... would it be using sendMultipartTextMessage()? There is a limit equal 160 signs while using sendTextMessage(), which is a standard single SMS message maximum length.

pzajdel
  • 15
  • 5