Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMS sent observer executes 3 times

I have defined the following service with an observer of messages sent. The problem is that when sending a message, I sense that is called 3 times onChange method of contentobserver. ¿Someone know tell me why?

Thanks

    public class DSMSService extends Service {
        private static final String CONTENT_SMS = "content://sms";

        private class MyContentObserver extends ContentObserver {
            ContentValues values = new ContentValues();
            int threadId;

            public MyContentObserver() {
                super(null);
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Log.v(TAG, "****************************** SMS change detected *************************************");
                Log.v(TAG, "Notification on SMS observer"); 
                // save the message to the SD card here
                Uri uriSMSURI = Uri.parse("content://sms");
                Cursor cur = getBaseContext().getContentResolver().query(uriSMSURI, null, null, null, null);
                // this will make it point to the first record, which is the last SMS sent
                cur.moveToNext();
                String content = cur.getString(cur.getColumnIndex("body"));

                Log.v(TAG, "content: " + content);
            }

            @Override
            public boolean deliverSelfNotifications() {
                return false;
            }
        }

        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

        @Override
        public void onCreate() {
            Log.v(TAG, "starting........");
            MyContentObserver contentObserver = new MyContentObserver();
            ContentResolver contentResolver = getBaseContext().getContentResolver();
            contentResolver.registerContentObserver(Uri.parse("content://sms"),true, contentObserver);
            DAO = new DAOaBlackList(getBaseContext());
        }

        @Override
        public void onDestroy() {
            Log.d(TAG, "stopping........");
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.v(TAG, "Received start id " + startId + ": " + intent);
            // We want this service to continue running until it is explicitly
            // stopped, so return sticky.
            return START_STICKY;
        }

        @Override
        public void onStart(Intent intent, int startid) {
            Log.v(TAG, "onStart........");
        }
    }
like image 555
itaravika Avatar asked Nov 23 '11 08:11

itaravika


2 Answers

What you want to do is check for the _id of the last item in the content://sms/sent uri inside onChange. You need to store the previous _id (maybe in a static global variable) and compare it to the _id of the last item (cursor.moveToLast())of the cursor after you query for content://sms/sent. If the _id is the same, you can choose to ignore the call to onChange. This multiple calls to onChange I believe is due to the sms being moved from folder to folder during sending - outbox, sent items, some other "invisible folder" (which we can't know exactly what, as this particular feature REALLY REALLY needs proper documentation). As you cannot listen to a more specific Uri than content://sms/sent you'll have to implement this checking for _id everytime you want to detect an sms being sent.

If the previous _id is different from the one in your static global variable, then you have an sms being sent.

like image 98
josephus Avatar answered Nov 03 '22 10:11

josephus


You have kept the Observer for the SMS database through URI. so whenever message is being send the database is updated and 3 of the column of that table is getting updated. so it will notify the observer for each of them. so it is being called for as many times as table data is updated.

like image 23
Dinesh Prajapati Avatar answered Nov 03 '22 10:11

Dinesh Prajapati