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

2

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!

Territerrible answered 24/5, 2013 at 10:47 Comment(2)
you have to override call method in your CP ... and then call function save from there ...Pasty
@Pasty That works! Thank you. If you add it as an answer I accept it.Territerrible
A
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.

Autolithography answered 9/1, 2014 at 0:26 Comment(5)
Note that the string comparison above should be done with method.equals() rather than ==.Disputation
One other comment, the call method is only available in API level 11 and above.Autolithography
It is interesting that that framework isn't doing any reflection to look for a method of that name, taking a String and Bundle. See the Android code: android.googlesource.com/platform/frameworks/base/+/refs/heads/…Splendid
Do you mean in API level 1-10 or in general?Autolithography
you sir are a hero!Volin
M
-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.

Melodics answered 24/5, 2013 at 10:58 Comment(1)
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.Territerrible

© 2022 - 2024 — McMap. All rights reserved.