11

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!

Community
  • 1
  • 1
OpenHaus
  • 97
  • 2
  • 9
  • 17

2 Answers2

23

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.

Andrew
  • 1,057
  • 11
  • 19
-3

If you want to redefine your own ContentProvider, you have to override these method:

  • onCreate()
  • query()
  • delete()
  • insert()
  • update()
  • getType()

But save() method is not inside the life cycle of a ContentProvider and cannot be called.

Jarvis
  • 1,527
  • 12
  • 17
  • Above is just a shortened version of my custom ContentProvider class. All mentioned methods are there. Please see my references. It is possible to define and call custom methods. I should have rather asked what the signature of the custom method should look like. – OpenHaus May 24 '13 at 11:00