Sending SMS programmatically to several people receiving a common error

I am currently trying to develop an SMS application and allow the user to send SMS to multiple users.

Since SMS is long, I have to use sendMultipartTextMessage, which is below

private void sendSMS(final String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; SmsManager sms = SmsManager.getDefault(); ArrayList<String> parts = sms.divideMessage(message); int messageCount = parts.size(); Log.i("Message Count", "Message Count: " + messageCount); ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); for (int j = 0; j < messageCount; j++) { sentIntents.add(sentPI); deliveryIntents.add(deliveredPI); } // ---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)); sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); } 

I came across a common error when I try to send it to several people in a loop like this and the message will not be sent. I suspect this is because the message is probably 3-4 parts, and the Android system cannot send the message on time even with a delay of 3000 milliseconds.

  for (int t = 0; t < array.length; t++) { System.out.println("temp: " + array[t].toString()); try { sendSMS(array[t].toString(), message); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } 

EDIT: The above code is in AsyncTask, which runs inside the Service.

+6
source share
2 answers

IHMO you are not using the right approach. I use another and it works. I am trying to explain:

You must keep the counter for all pending intentions, because if the SMS you send has two parts, you will receive two RESULT_OK (or RESULT_ERROR_ *), and only when you get the results for all parts, you should continue sending the next SMS. I don't like the idea of ​​blocking a thread ... maybe that is why you get weird errors.

I reorganized your code using my approach:

 private int mMessageSentParts; private int mMessageSentTotalParts; private int mMessageSentCount; private void startSendMessages(){ registerBroadCastReceivers(); mMessageSentCount = 0; sendSMS(array[mMessageSentCount].toString(), message); } private void sendNextMessage(){ if(thereAreSmsToSend()){ sendSMS(array[mMessageSentCount].toString(), message); }else{ Toast.makeText(getBaseContext(), "All SMS have been sent", Toast.LENGTH_SHORT).show(); } } private boolean thereAreSmsToSend(){ return mMessageSentCount < array.length; } private void sendSMS(final String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; SmsManager sms = SmsManager.getDefault(); ArrayList<String> parts = sms.divideMessage(message); mMessageSentTotalParts = parts.size(); Log.i("Message Count", "Message Count: " + mMessageSentTotalParts); ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); for (int j = 0; j < mMessageSentTotalParts; j++) { sentIntents.add(sentPI); deliveryIntents.add(deliveredPI); } mMessageSentParts = 0; sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents); } private void registerBroadCastReceivers(){ registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: mMessageSentParts++; if ( mMessageSentParts == mMessageSentTotalParts ) { mMessageSentCount++; sendNextMessage(); } 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)); 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)); } 
+19
source

I got the same "General Crash" error that occurred using this code. But I used the array [mMessageSentCount] .toString (). Trim () . Then he solved my problem and her work was excellent. Thanks

+2
source

All Articles