1

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!!

Marline
  • 269
  • 3
  • 10
  • Have you figured this out in the end? – Nikola Feb 17 '18 at 13:38
  • unfortunately not. I implemented an in app solution without interfacing with the BlockedNumberContract. – Marline Feb 20 '18 at 23:50
  • Thanks. I'm interested - could you please possibly go a bit deeper in explaining how you achieved it? – Nikola Feb 21 '18 at 10:25
  • 1
    I created a SQLite table to keep track of the contacts the user wants to block. I also upload these blocked contacts to our server and the server determines whether or not to pass on the incoming call to phone depending on whether that contact has been blocked by the user. – Marline Feb 21 '18 at 21:58
  • @Marline Can you share code for setting app as default phone app if possible? Thanks – Krish Jun 27 '18 at 07:43
  • @Krishna i set the app to be the default messaging app, is that what you are looking for? – Marline Jul 02 '18 at 18:22
  • @Marline Actually I'm looking for code to set app as default phone app but if you can share code of setting app as default messaging app then it may give me some hint. Thanks – Krish Jul 03 '18 at 05:27
  • @Krishna here it is: Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); startActivity(intent); – Marline Jul 05 '18 at 22:55

0 Answers0