How do I call custom method in ContentProvider through ContentResolver and access Bundle afterwards?

OpenHaus picture OpenHaus · May 24, 2013 · Viewed 7.9k times · Source

I have a custom method save() in my custom ContentProvider class MyContentProvider which I want to call through the ContentResolver. The objective is to pass an POJO as a Bundle through to MyContentProvider.

I am using the call method as mentioned here and defined here.

I do not get any errors. The method is just not accessed.

The (shortened), custom ContentProvider with the custom method looks like this:

public class MyContentProvider extends ContentProvider {

    public void save() {

        Log.d("Test method", "called");
    }
}

I call it like this:

ContentResolver contentResolver = context.getContentResolver();
Bundle bundle = new Bundle();
bundle.putSerializable("pojo", getPojo());
contentResolver.call(Contracts.CONTENT_URI, "save", null, bundle);

Why is the save method never called and if I get to this point how do I access the called Uri and the Bundle in the save() method? I could not find any reference for this anywhere on SO or the web.

Thank you for your answers!

Answer

Andrew picture Andrew · Jan 9, 2014

I've just been playing with this to get a custom function working. As noted in the comment on your question, the key is implementing the call() method in the content provider to handle the various methods you might pass in.

My call to the ContentResolver looks like this:

ContentResolver cr = getContentResolver();

cr.call(DBProvider.CONTENT_URI, "myfunction", null, null);

Inside the ContentProvider, I've implemented the call function and it check the method name passed in:

@Override
public Bundle call(String method, String arg, Bundle extras) {
    if(method.equals("myfunction")) {
        // Do whatever it is you need to do
    }
    return null;
}

That seems to work.