1

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");

            }
        }
user2672165
  • 2,986
  • 19
  • 27
vipin
  • 2,851
  • 4
  • 18
  • 32
  • check this: http://stackoverflow.com/questions/7550178/intercepting-outgoing-sms/7790755#7790755 – Vineet Shukla Mar 16 '12 at 05:36
  • http://stackoverflow.com/questions/5808577/android-listen-outgoing-sms-or-sent-box – Calvin Mar 16 '12 at 05:38
  • Duplicate of http://stackoverflow.com/questions/990558/android-broadcast-receiver-for-sent-sms-messages – Dheeraj Vepakomma Mar 16 '12 at 06:13
  • is there any problem in my code if yes plz let me know – vipin Mar 19 '12 at 06:37
  • see this question [link][1] [1]: http://stackoverflow.com/questions/8991339/observe-sms-sending-app-in-emulator/9582426#9582426 – Girish Bhutiya Mar 20 '12 at 13:47
  • there is an difference between both i was asking for phone and http://stackoverflow.com/questions/8991339/observe-sms-sending-app-in-emulator/9582426#9582426 was asked for emulator @Girish bhutia – vipin Aug 28 '12 at 11:55

2 Answers2

2

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");

                }
            }
vipin
  • 2,851
  • 4
  • 18
  • 32
1

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);
  • with calls i have done but the only problem is in getting sent sms broadcast – vipin Mar 19 '12 at 05:23
  • have you checked http://stackoverflow.com/questions/5808577/android-listen-outgoing-sms-or-sent-box –  Mar 19 '12 at 06:11
  • yes i have checked and i am not getting logs please take a look at my code – vipin Mar 19 '12 at 06:24
  • i see you have done the content observer part... see the two new link i have added...it may help you out –  Mar 19 '12 at 07:18
  • this line was causing problem madcontentResolver.registerContentObserver(Uri.parse("content:// sms/out"),true, myObserver); – vipin Mar 20 '12 at 13:26