Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android content provider AUTHORITIES

What is the reason for content provider authorities?

How/why do I want to use them other than I HAVE to declare them in the manifest?

I've tried to do my homework on this question and cannot find a decent, cohesive discussion on this topic. Here is the best I could find (in additi on to the four books on Android development I own):

  1. https://stackoverflow.com/search?q=content+provider+authority
  2. Content Providers, Authority and and URI matching
  3. Get a list of available Content Providers
  4. http://developer.android.com/guide/topics/manifest/provider-element.html
  5. http://developer.android.com/guide/topics/providers/content-provider-creating.html
like image 883
JDOaktown Avatar asked Jun 12 '14 16:06

JDOaktown


2 Answers

The Authority is used to interact with a particular content provider, that means it must be unique. That's why is a good practice to declare it as your domain name (in reverse) plus the name of the package containing the provider, that way is less likely that other developer creates an app with a content provider declaring the same authority.

You declare it in the manifest so your app and other apps (if you let them) can manipulate data through your content provider in the form of a uri:

content://authority-name/data-in-the-provider

It works similar to domains in http urls:

http://domain-name/data-in-the-site
like image 156
ILovemyPoncho Avatar answered Oct 19 '22 09:10

ILovemyPoncho


I am also looking for explanation and to add to answer provided by ILovemyPoncho, I hit this particular answer and I quote:

and what exactly is android:authorities asking for?

A system wide unique identifier for your provider. Or better worldwide unique. All providers are registered with the system and they need to be unique or the second app that wants to use the same name can't be installed.

You use that string in the end to communicate with your provider via an Uri like

Uri uri = Uri.parse("content://" + "your.authoritiy.string")

Let's put it this way: There is an invisible hand that facilitates your request to your app's ContentProvider.

For example:

    Uri uri = mContext.getContentResolver().insert(NotifireContentProvider2.NOTE_URI, values);

Basically, what you are saying here to the Android OS is insert data given the URI containing the authority you have defined in the XML. The OS will search for this particular content provider and send the request to it. You insert method on the ContentProvider will be called and you must match the URI to handle it accordingly.

Also, what if, you're content provider is so simplistic that others have similar authority as well. I haven't encountered those two problem I mentioned but I reckon it won't be pleasant.

Authority is there to make sure that the OS understand which provider will provide the data to the requesting app and to make sure that is the provider providing it.

like image 42
Neon Warge Avatar answered Oct 19 '22 07:10

Neon Warge