Sent SMS observer performs 3 times

I defined the following service with an observer of sent messages. The problem is that when I send a message, I feel that it is called 3 times by the method of change contentobserver. Does anyone know 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........"); } } 
+4
source share
2 answers

What you want to do is check the _id last element in content://sms/sent uri inside onChange. You need to save the previous _id (possibly in a static global variable) and compare it with the _id of the last element ( cursor.moveToLast() ) of the cursor after the request for content://sms/sent . If _id is the same, you can ignore the onChange call. These are a few onChange calls, which, I believe, are associated with moving sms from folder to folder during sending - outgoing, sent items, some other “invisible folders” (which we cannot know for sure, because this feature REALLY REALLY needs in proper documentation). Since you cannot listen to a more specific Uri than content://sms/sent , you will have to implement this check for _id every time you want to detect sent sms.

If the previous _id is different from that in your static global variable, then you send sms.

+6
source

You saved Observer for the SMS database through the URI. therefore, whenever a message is sent, the database is updated and the 3 columns of this table are updated. therefore, he will notify the observer of each of them. therefore, it is called as many times as the table data is updated.

+3
source

Source: https://habr.com/ru/post/1382656/


All Articles