Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
541 views
in Technique[技术] by (71.8m points)

android - 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........");
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...