Android - determining the reasons for the failure of sending SMS

I have a reminder application that will send an SMS to notify the user that the reminder time has passed. It works well. However, I tested what happens when the phone was asleep and missed a reminder.

I use AlarmManager to configure alarms corresponding to reminder times. My log shows that the alarm went off when the phone rebooted and an attempt to send an SMS occurs, but the SMS is never received.

So the question is, is there a way to debug, why is SMS not sent?

My current code sets ContentObserver when an alarm event occurs:

  private void registerToListenForSentSMS() { MessageSentListener smsObeserver = new MessageSentListener(new Handler()); ContentResolver contentResolver = TheEveryOtherAlarmAppApplication.getAppContext().getContentResolver(); contentResolver.registerContentObserver(Uri.parse("content://sms"), true, smsObeserver); } 

MessageSentListener receives a notification that an SMS event has occurred:

 public class MessageSentListener extends ContentObserver { public MessageSentListener(Handler handler) { super(handler); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Log.d(Constants.ALARM_APP_LOG_TAG, "Something happend"); ContentResolver contentResolver = AlarmAppApplication.getAppContext().getContentResolver(); contentResolver.unregisterContentObserver(this); } } 

But this may be caused by SMS events not related to my application. In any case, this does not bring me closer to a solution - I would like to know what went wrong when nothing happens!

If the SMS message is not sent, I can either resend it or use another notification method, for example, email, etc.

+6
source share
1 answer

You will need a broadcast receiver and a pending intent to receive a notification of failed sms.

Below code snippet will help you

 //---sends an SMS message to another device--- 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 context, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_RADIO_OFF: Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(SENT)); //---when the SMS has been delivered--- registerReceiver(new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); 
+5
source

Source: https://habr.com/ru/post/917289/


All Articles