Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Practical way to find out if SMS has been sent

Tags:

android

I am interested in how I can figure out if SMS has been sent from the device.

In order to get notification when SMS is recieved, we use a broadcaster with:

android.provider.Telephony.SMS_RECEIVED

Important to mention that I do not send SMS from my app, I just should listen when SMS is sent from the device.

May be I should listen to some Content provider (which somehow related with SMS) and react for that change. Any ideas how I can achieve that?

like image 593
Jviaches Avatar asked Mar 01 '12 16:03

Jviaches


People also ask

How do you know if SMS has been delivered?

To enable delivery report on Android go to Settings > Text Message (SMS) Settings and turn on the Delivery Reports option. From that point on, your Android device will start receiving delivery reports for SMS messages, notifying you of the current state status of the text message.

How do I know if text message was received Android?

Turn on Delivery Receipts to find out if your text message was delivered to the recipient. (This option doesn't tell you if the message was read.) On newer phones, open the Messages app and go to Settings > Advanced > Get SMS delivery reports.

Why do some text messages say delivered?

"Delivered" is a good sign. It means that the text is ready and waiting for them to open it. The service provider has now received confirmation that the message has been delivered to the recipient's phone.

Why do some text messages say Read and others don't on Android?

Some people don't have receipts enabled, and it's usually pretty easy to tell. If you send messages to them and they respond to you, but there is still no read receipt, odds are good that their read receipts are disabled. With Community, there are typically not regular read receipts that go along with text messages.


2 Answers

Yes, It is possible to listen SMS ContentProvider by using ContentObserver

Here is my example for Outgoing SMS:

First register a ContetObserver with 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   
            }
      }
  }

}
like image 150
ρяσѕρєя K Avatar answered Oct 05 '22 03:10

ρяσѕρєя K


use the following method to send sms as well as check whether the sms get 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);

    }
like image 26
Manikandan Avatar answered Oct 05 '22 02:10

Manikandan