短信发送观察者执行3次

发布于 2024-12-17 17:16:23 字数 2504 浏览 2 评论 0原文

我已经使用发送消息的观察者定义了以下服务。问题是,当发送消息时,我感觉 contentobserver 的 onChange 方法被调用了 3 次。 ¿有人知道告诉我为什么吗?

谢谢

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-12-24 17:16:23

您想要做的是检查 onChange 内 content://sms/sent uri 中最后一项的 _id。您需要存储之前的 _id(可能在静态全局变量中),并在查询 content 后将其与光标的最后一项 (cursor.moveToLast()) 的 _id 进行比较//sms/已发送。如果_id相同,您可以选择忽略对onChange的调用。我相信对 onChange 的多次调用是由于在发送过程中短信从一个文件夹移动到另一个文件夹 - 发件箱、已发送项目、一些其他“不可见文件夹”(我们无法确切知道是什么,因为这个特定功能确实需要适当的文档)。由于您无法侦听比 content://sms/sent 更具体的 Uri,因此每次您想要检测正在发送的短信时,您都必须对 _id 进行检查。

如果前一个 _id 与静态全局变量中的不同,则您将发送一条短信。

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.

心凉 2024-12-24 17:16:23

您已经通过 URI 保留了 SMS 数据库的观察者。因此,每当发送消息时,数据库都会更新,并且该表的 3 列也会更新。所以它会通知每个人的观察者。因此,随着表数据的更新,它会被调用多次。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文