I am creating a app for android which sends sms using gateways like "way2sms.com", "fullonsms.com", i have succeeded in sending messages from those gateways, now i want to take another step, i.e "SMS DELIVERY REPORT". I have a basic idea about how will i do this. When i will send sms to the number selected by the user after that i will send one sms back to user with some formatted text for e.g. "SMSDELIVERYREPORT". What i want to do is that when the user receives such message instead of normal notification which we get when we receive normal sms, the application of create a Alert Dialog saying "Message successfully delivered." Please guide me in fulfilling this task. and also how can i make sure that this messages do not go in users normal message inbox.
2 Answers
This code would cover all the possible cases of your SMS sending.
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
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 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));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
And here is a great tutorial!
-
i am sending sms using "way2sms.com" or "fullonsms.com", can i use this code? Sep 2, 2012 at 10:55
-
I did not understand. You are saying that you are using their APIs to send sms ?– SwayamSep 2, 2012 at 10:58
-
Then I believe you can! Works pretty much the same way. I am not 100% sure though! Better you give it a try yourself but I think it should work!– SwayamSep 2, 2012 at 11:02
I used this tutorial, when i was creating something similiar!
Just jump to Handle Received SMS!
-
-
simply add abortBroadcast(); into your broadcast reciever (after checking if it's the right sms) Sep 2, 2012 at 10:56