Practical way to find out if an SMS has been sent

I am wondering how I can understand if an SMS was sent from the device.

To receive a notification when receiving SMS, we use a broadcasting company with:

android.provider.Telephony.SMS_RECEIVED 

It is important to note that I do not send SMS from my application, I just have to listen when SMS is sent from the device.

Maybe I should listen to some content provider (which is somehow connected with SMS) and respond to this change. Any ideas how I can achieve this?

+7
source share
3 answers

Yes, you can listen to SMS ContentProvider using ContentObserver

Here is my example for outgoing SMS:

First register a ContetObserver with the content: // sms /

  public class Smssendservice extends Service{ @Override public void onCreate() { SmsContent content = new SmsContent(new Handler()); // REGISTER ContetObserver this.getContentResolver(). registerContentObserver(Uri.parse("content://sms/"), true, SMSObserver); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } 

SMSObserver.class

  public class SMSObserver extends ContentObserver { private Handler m_handler = null; public SMSObserver(SMSLogger handler){ super(handler); m_handler = handler; } @Override public void onChange(boolean selfChange) { super.onChange(bSelfChange); Uri uriSMSURI = Uri.parse("content://sms"); Cursor cur = this.getContentResolver().query(uriSMSURI, null, null, null, null); cur.moveToNext(); String protocol = cur.getString(cur.getColumnIndex("protocol")); if(protocol == null) { //the message is sent out just now } else { //the message is received just now } } } } 
+16
source

use the following method to send sms, and also check if sms are delivered or not.

 send.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String phoneNo = "Phone number to sent"; String message = "Your message"; if (phoneNo.length() > 0 && message.length() > 0) { TelephonyManager telMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); int simState = telMgr.getSimState(); switch (simState) { case TelephonyManager.SIM_STATE_ABSENT: displayAlert(); break; case TelephonyManager.SIM_STATE_NETWORK_LOCKED: // do something break; case TelephonyManager.SIM_STATE_PIN_REQUIRED: // do something break; case TelephonyManager.SIM_STATE_PUK_REQUIRED: // do something break; case TelephonyManager.SIM_STATE_READY: // do something sendSMS(phoneNo, message); // method to send message break; case TelephonyManager.SIM_STATE_UNKNOWN: // do something break; } } else { Toast.makeText(getBaseContext(), "Please enter both phone number and message.", Toast.LENGTH_SHORT).show(); } } private void displayAlert() { // TODO Auto-generated method stub new AlertDialog.Builder(YourActivity.this) .setMessage("Sim card not available") .setCancelable(false) // .setIcon(R.drawable.alert) .setPositiveButton("ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Log.d("I am inside ok", "ok"); dialog.cancel(); } }) .show(); } }); private void sendSMS(String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; PendingIntent sentPI = PendingIntent.getBroadcast(YourActivity.this, 0, new Intent(SENT), 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(YourActivity.this, 0, new Intent(DELIVERED), 0); // ---when the SMS has been sent--- final String string = "deprecation"; registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context arg0, Intent arg1) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(YourActivity.this, "SMS sent", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Toast.makeText(YourActivity.this, "Generic failure", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NO_SERVICE: Toast.makeText(YourActivity.this, "No service", Toast.LENGTH_SHORT).show(); break; case SmsManager.RESULT_ERROR_NULL_PDU: Toast.makeText(YourActivity.this, "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(YourActivity.this, "SMS delivered", Toast.LENGTH_SHORT).show(); break; case Activity.RESULT_CANCELED: Toast.makeText(YourActivity.this, "SMS not delivered", Toast.LENGTH_SHORT).show(); break; } } }, new IntentFilter(DELIVERED)); SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } 
+8
source

One thing that comes to mind is to use logcat. Each time an SMS is sent / received, a specific entry appears. Just connect your phone to debugging and testing, and you will see a specific entry.

I should mention that this method is executed from your application, so you can use it in your code.

Take this . There may be other methods, but it looks pretty easy and expertly.

+1
source

All Articles