Question

I have a ContentObserver on my application to check when a contact is edited, I have implemented it with the following code, it works on devices with lower android API than 17 but not 17 and higher.

observer = new MyContentObserver();
getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, observer);


private class MyContentObserver extends ContentObserver
{
    public MyContentObserver()
    {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange)
    {
        super.onChange(selfChange);
        cache.clearCache();
    }
}
Was it helpful?

Solution

I believe the problem is that newer API levels utilize an overload of the onChange() method that takes two parameters: a boolean and a Uri. Try changing your code as follows:

private class MyContentObserver extends ContentObserver
{ 
    public MyContentObserver() 
    { 
        super(null); 
    } 

    @Override 
    public void onChange(boolean selfChange)
    {  
        onChange(selfChange, null); 
    }

    @Override
    public void onChange(boolean selfChange, Uri uri)
    {
        cache.clearCache();
    } 
}

You could call cache.clearCache(); in both methods directly, but this way will allow any possible future modifications to be made in just one place. Also, the calls to super.onChange() are unnecessary, as the overridden methods are empty in the super class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top