SMS sent observer executes 3 times

前端 未结 2 2053
梦谈多话
梦谈多话 2020-12-17 02:32

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 content

2条回答
  •  执笔经年
    2020-12-17 03:19

    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.

提交回复
热议问题