vipin
vipin

Reputation: 3001

Receive broadcast when send an sms

How to receive broadcast when a user sends SMS from his Android phone? I am creating an application which is taking track of sent SMS and calls. I am done with the calls part, please help me with the SMS. Note that sms are sent by the phone not any application.

----------//solution-----------

  public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(VIEW_RESOURCE_ID);

            SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
            ContentResolver contentResolver = this.getContentResolver();
            contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
        }


    public class SendSmsObserver extends ContentObserver {

            public SendSmsObserver(Handler handler) {
                super(handler);
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                // save the message to the SD card here

                 Log.d("sent sms", "one text send");

            }
        }

Upvotes: 1

Views: 4604

Answers (2)

vipin
vipin

Reputation: 3001

I found the answer

public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(VIEW_RESOURCE_ID);

                SendSmsObserver smsObeserver = (new SendSmsObserver(new Handler()));
                ContentResolver contentResolver = this.getContentResolver();
                contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);
            }


        public class SendSmsObserver extends ContentObserver {

                public SendSmsObserver(Handler handler) {
                    super(handler);
                }

                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    // save the message to the SD card here

                     Log.d("sent sms", "one text send");

                }
            }

Upvotes: 2

user671253
user671253

Reputation:

You could build on CallLog. The CallLog provider contains information about placed and received calls.

The Following code can work

Cursor c = null; try {
    c = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
    if (c != null && c.moveToFirst()) {
        do {
            int duration = c.getInt(c.getColumnIndex(CallLog.Calls.DURATION));
            // do something with duration
        } while (c.moveToNext());
    } } finally {
    if (c != null) {
        c.close();
    } }

--------------------------ADDED NEW SOLUTION------------------------

Have a look at: http://groups.google.com/group/android-developers/browse_thread/thread/9bc7d7ba0229a1d2

and : http://code.google.com/p/android/issues/detail?id=914

Basically, you can do it by registering a content observer on the SMS message store.Try this:

  ContentResolver contentResolver = this.getContentResolver();
        contentResolver.registerContentObserver(Uri.parse("content://sms"),true, smsObeserver);

Upvotes: 1

Related Questions