Hi Guys I have made simple app to send multiple SMS on android, and I cannot find known which sms is Sent/Delivered, because the delivery notification does not tell for which it is.
here is my code :
this.destination = data[0];
this.body = data[1];
PendingIntent piSend = PendingIntent.getBroadcast(aCtx, 0, new Intent(Cons.SMS_SENT),0);
PendingIntent piDelivered = PendingIntent.getBroadcast(aCtx, 0, new Intent(Cons.SMS_DELIVERED), 0);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(destination, null, body, piSend, piDelivered);
Then I use broadcast receiver to get delivery status
public class DeliverSMSBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(context, "SMS delivered",
Toast.LENGTH_SHORT).show();
Log.d(Cons.TAGS+" RK","RESULT_OK=> DELIVER");
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "SMS not delivered",
Toast.LENGTH_SHORT).show();
Log.d(Cons.TAGS+" RK","RESULT_CANCELED");
break;
}}
}
And Here Is my activity :
public class SenderPage extends Activity {
private EditText txtRecepients = null;
private EditText inputSMSText = null;
private Button btnSendSMS = null;
private final BroadcastReceiver outgoingSMSBR = new OutgoingSMSBroadcastReceiver();
private final BroadcastReceiver deliverSMSBR = new DeliverSMSBroadcastReceiver();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.senderpage);
Log.i(Cons.TAGS, "Sender Page Start");
this.setTitle("Send Message");
this.txtRecepients = (EditText) findViewById(R.id.txtRecepients);
this.inputSMSText = (EditText) findViewById(R.id.inputSMSText);
this.btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
txtRecepients.setText("087722079905"); //087722079905 //081214571542
btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(SenderPage.this, "Clicked send", Toast.LENGTH_SHORT).show();
String txtRecepients = SenderPage.this.txtRecepients.getText().toString();
String inputSMSText = SenderPage.this.inputSMSText.getText().toString();
new Sender(SenderPage.this,Cons.TAGS).execute(txtRecepients, inputSMSText);
}
});
//txtRecepients.getText().toString();
}
@Override
protected void onResume() {
registerReceiver(outgoingSMSBR, new IntentFilter(Cons.SMS_SENT));
registerReceiver(deliverSMSBR, new IntentFilter(Cons.SMS_DELIVERED));
super.onResume();
}
@Override
protected void onPause() {
unregisterReceiver(outgoingSMSBR);
unregisterReceiver(deliverSMSBR);
super.onPause();
}
}
And the Problem is when I sent more than One SMS then I get the delivery information I cannot make sure which SMS is delivered ?
Finally I got the answer by added intent extras to pending intent with sms create datetime, then update sms status based on datetime that sent by extras data, here is the snippet :
Intent sentIntent = new Intent(Cons.SMS_SENT);
sentIntent.putExtra("createdTime", createdTime);
Intent deliveryIntent = new Intent(Cons.SMS_SENT);
deliveryIntent.putExtra("createdTime", createdTime);
then I can identify which sms is sent/delivery by created time
public void onReceive(Context context, Intent intent) {
String smsDateTimeAsID = intent.getStringExtra("createdTime");
}
Cons.SMS_SENT and Cons.SMS_DELIVERED Values
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
来源:https://stackoverflow.com/questions/15835052/how-to-know-which-sms-is-sent-delivered-from-android-broadcastreceiver