Last active
December 17, 2018 21:45
-
-
Save ponnamkarthik/7bbfcd987b23ec646de39d3c423ddf52 to your computer and use it in GitHub Desktop.
Send Message in Android with Delivery and Sent Report
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void sendSms() { | |
try { | |
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); | |
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT)); | |
registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED)); | |
SmsManager sms = SmsManager.getDefault(); | |
sms.sendTextMessage("121", null, final_url, sentPI, deliveredPI); | |
} catch (Exception e) { | |
sendSms(); | |
} | |
} | |
class DeliverReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent arg1) { | |
switch (getResultCode()) { | |
case Activity.RESULT_OK: | |
Snackbar.make(view, "Delivered", | |
Snackbar.LENGTH_SHORT).show(); | |
break; | |
case Activity.RESULT_CANCELED: | |
Log.w("GHM", "SMS Not Delivered"); | |
sendSms(); | |
break; | |
} | |
} | |
} | |
class SentReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent arg1) { | |
switch (getResultCode()) { | |
case Activity.RESULT_OK: | |
Snackbar.make(view, "Sent", | |
Snackbar.LENGTH_SHORT).show(); | |
break; | |
case SmsManager.RESULT_ERROR_GENERIC_FAILURE: | |
Log.w("GHM", "Generic failure"); | |
sendSms(); | |
break; | |
case SmsManager.RESULT_ERROR_NO_SERVICE: | |
Log.w("GHM", "No service"); | |
sendSms(); | |
break; | |
case SmsManager.RESULT_ERROR_NULL_PDU: | |
Log.w("GHM", "Null PDU"); | |
sendSms(); | |
break; | |
case SmsManager.RESULT_ERROR_RADIO_OFF: | |
Log.w("GHM", "Radio off"); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment