Can't Unregister a ContentObserver

408 views Asked by At

So, This is the Code im using in a Service :

  1. The Service Start & Run this Code :

      SmsObserver smsSentObserver = new SmsObserver(new Handler(), this);
      this.getContentResolver().registerContentObserver(Uri.parse("content://sms"), true,            
      smsSentObserver); 
    

Then the Service stopSelf()

2.The Service Start & Run this Code :

      SmsObserver smsSentObserver = new SmsObserver(new Handler(), this);
      this.getContentResolver().unregisterContentObserver(smsSentObserver);

Then the Service stopSelf(),

Problem : the ContentObserver is Not unregistred & keep to receive the onChange Method

Additionnal info : Everything worked fine before, it is related to KitKat or what is wrong Here ?

Thanks

1

There are 1 answers

5
zozelfelfo On BEST ANSWER

AFAIK you are c reating a different object for your Observer so maybe is that the reason it is giving you trouble.

Keep a reference of your SmsObserver in your service, and use that variable to call to unregisterContentObserver.

That should solve your problem