In this following code message is a Message class array which contains 100+ phonenumbers and sms messages.
for(Message msg: message) {
String msgString = msg.getSMSText1() + " " + msg.getTransporterNo() + "\n" + msg.getSMSText2();
smsManager.sendTextMessage(msg.getPhoneNo(), null, msgString, piSent, null);
}
And this code for receiving broadcast after sending a message:
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), PendingIntent.FLAG_UPDATE_CURRENT);
BroadcastReceiver smsSentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String result = "";
switch(getResultCode()) {
case Activity.RESULT_OK:
result = "Transmission successful";
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
result = "Transmission failed";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
result = "Radio off";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
result = "No PDU defined";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
result = "No service";
break;
}
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_SHORT).show();
}
};
registerReceiver(smsSentReceiver, new IntentFilter("SMS_SENT"));
Is there a way to pass additional data during smsManager.sendTextMessage? I'd like to pass additional data like message_id, and phonenumber so that I can find out which message was success or not.
You can attach the necessary data as extras on the Intent
object. For example:
Intent sentIntent = new Intent("SMS_SENT");
sentIntent.putExtra("message_id", id);
sentIntent.putExtra("phone_number", number);
PendingIntent piSent = PendingIntent.getBroadcast(this, 0, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Then you can retrieve them from the Intent
passed into the onReceive()
method of your BroadcastReceiver
:
int id = intent.getIntExtra("message_id", -1);
String number = intent.getStringExtra("phone_number");
Also, as David Wasser reminded me below, to ensure a new PendingIntent
is issued each time through the loop, you'll need to provide each a unique request code - the second argument in the getBroadcast()
call. If the message IDs are unique, you can use those. Otherwise, a loop counter should suffice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.