Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is getContentResolver().call() and how to use it

I have read the documentation on the Android ContentResolver

I have also searched for a suitable example to no avail.

According to the documentation, there is a method call that can be used as a way to get access to custom provider methods when the standard content provider methods are insufficient:

final Bundle     call(Uri uri, String method, String arg, Bundle extras)
Call a provider-defined method.

so in my code I execute:

getContentResolver().call(uri, method, arg, extras);

but it always returns null bundle. In fact, the method in the provider never gets called.

Further research points to a (perceived) discrepancy of the contract where the RESOLVER has a uri argument with no equivalent PROVIDER parameter:

Bundle   call(String method, String arg, Bundle extras)
Call a provider-defined method.

I am obviously not understanding something. Can anyone point me in the correct direction?

like image 528
Roy Hinkley Avatar asked Feb 17 '23 04:02

Roy Hinkley


2 Answers

Further research points to a discrepancy of the contract where the RESOLVER has a uri argument with no equivalent PROVIDER parameter

That's the way they wrote it. The Uri is simply to identify the ContentProvider -- the ContentProvider knows who it is and therefore does not need the Uri.

the provider method does not allow @Override annotation

Yes, it does, as you can see in this sample ContentProvider:

  @Override
  public Bundle call(String method, String arg, Bundle extras) {
    if (SET_KEY_METHOD.equals(method) && arg != null) {
      key=arg;
    }

    return(null);
  }

However, your build target (e.g., in Eclipse, Project > Properties > Android) must be set to API Level 11 or higher.

The corresponding call() from the client looks like:

getContentResolver().call(Provider.Constants.CONTENT_URI,
                          Provider.SET_KEY_METHOD, "sekrit", null);

Yes. I have a method in the provider that is declared 'public' that is passed into the contentresolver argument 'method.'

That's not how it works. call() on ContentResolver calls call() on your ContentProvider.

like image 99
CommonsWare Avatar answered Feb 18 '23 18:02

CommonsWare


To answer your second question, my guess is that the ContentProvider version of call() does not need a Uri argument because, unlike a ContentResolver, it doesn't need to find a ContentProvider; it calls the method on itself.

like image 38
Code-Apprentice Avatar answered Feb 18 '23 18:02

Code-Apprentice