Use ContentObserver with BlockedNumberContract

337 views Asked by At

I have a VOIP calling and texting app. I need to be able to monitor changes to the native blocked number list so I can maintain that list on my server.

Following the documentation on BlockedNumberContract at https://developer.android.com/reference/android/provider/BlockedNumberContract.html it says to implement a ContentObserver to monitor when the user makes a change to the list.

My app asks the user to set it as the default Messaging app, so that covers the part in the documentation thats says that

Only the system, the default SMS application, and the default phone app (See getDefaultDialerPackage()), and carrier apps (See CarrierService) can read, and write to the blockednumber provider.

I created a service and registered it in the manifest. The service is started properly and I am able to see the log that I have in onStartCommand()

public class MonitorBlockedContactsListService extends Service
{
    private static CallBlockedListContentObserver myObserver;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("NewApi")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("starting MonitorBlockedContactsListService");
        myObserver = new CallBlockedListContentObserver(new Handler());
        getContentResolver().registerContentObserver
                (BlockedNumberContract.BlockedNumbers.CONTENT_URI, true, myObserver);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        getContentResolver().unregisterContentObserver(myObserver);
        super.onDestroy();
    }
}

Here is the implementation for CallBlockedListContentObserver

public class CallBlockedListContentObserver extends ContentObserver
{
    public CallBlockedListContentObserver(Handler handler)
    {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange) {
        this.onChange(selfChange, null);
        Log.i("CallBlockedListContentObserver", "first method");
    }

    @Override
    public void onChange(boolean selfChange, Uri uri) {
        Log.i("CallBlockedListContentObserver", "2nd method uri= "+uri);
    }

    @Override
    public boolean deliverSelfNotifications() {
        return true;
    }
}

My problem is that the logs inside the 2 onChange() methods never get printed when I block a contact from the phonebook which leads me to think that the ContentObserver is not tracking the changes. I am currently testing on an Android 7 device so there are no issues with BlockedNumberContract being a new API. Can anyone tell me what I'm doing wrong? Why is my app not getting notified when I block a contact? Thanks!!

0

There are 0 answers