Search code examples
androidsmsandroid-contentproviderandroid-contentresolver

Is it possible to replace by my own ContentProvider Android's default one?


I'm considering possibility to replace Android's default SMS ContentProvider with my own one.

I'm talking about those which is called after:

context.getContentResolver().query(Uri.parse("content://sms/"),....);

I would dare to ask: is it possible?


Solution

  • No, That is internally used by the SMS messaging application AND the telephony layer of Android.

    Replacing any in-built content providers is guaranteed to break Android - that is a given!

    But what you can do is create your own content provider and craft your application to use your own instead.

    If you're talking about monitoring the sms content provider, what you can do is use a ContentObserver to watch on the sms content provider and forward the changes made to the sms content provider to your own.

    Here's an example of such scenario, everytime a change is made, the onChange gets fired, it is within there, that relaying to your own custom content provider will suffice.

    private class MySMSContentObserver extends ContentObserver{
        public MySMSContentObserver() {
            super();
        }
    
        @Override
        public boolean deliverSelfNotifications() { 
            return true; 
        }
    
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            // This is where the change notifications gets received!
        }
    }
    
    // For example
    MySMSContentObserver contentSMSObserver = new MySMSContentObserver();
    //
    context.getContentResolver().registerContentObserver (
            "content://sms", 
            true, 
            contentSMSObserver);
    

    Also, do not forget to unregister the content observer when the application is finished, i.e.:

    context.getContentResolver().unregisterContentObserver(contentSMSObserver);