Question

This is a deep magic question. I understand that a call to a ContentResolver method takes a URI specific to the ContentProvider, but how does android actually make the association?

I am guessing that any URI matching the authority provided with the ContentProvider in the AndroidManifest.xml is involved. Is the request sent to every provider containing that authority? If I try to create providers whose authority prefixes another authority will that be a problem?

Is there a way to see if the ContentProvider is running? I'm thinking maybe a dummy response on the getType() method would indicate liveness.

Was it helpful?

Solution

Class ContentResolver maintains a mapping from Content Authorities to ContentProvider classes. The data for that mapping comes from the <provider> elements of the various installed applications' AndroidManifest.xml files. ContentResolver uses this mapping to identify which Provider class is the right one to use for a given URI that comes in. Think of ContentResolver as being sort of like DNS. It figures out which server (provider) is the right one to answer your query.

Only one ContentProvider will match, because contentAuthorities (the "domain name" part of the content: type uri) are required to be unique. They are not hierarchical. Treat them as a unique string which must exactly match. The reason they look hierarchical is to allow an easy way of guaranteeing uniqueness, akin to the way Java package names are ensured to be unique.

Per the 'Description:" section for the tag documentation:

The Android system identifies content providers by the authority part of a content: URI. For example, suppose that the following URI is passed to ContentResolver.query():

content://com.example.project.healthcareprovider/nurses/rn

The content: scheme identifies the data as belonging to a content provider and the authority (com.example.project.healthcareprovider) identifies the particular provider. The authority therefore must be unique. Typically, as in this example, it's the fully qualified name of a ContentProvider subclass. The path part of a URI may be used by a content provider to identify particular data subsets, but those paths are not declared in the manifest

As for what happens when you make a provider with a contentAuthority that's identical to another one... Well, stuff breaks. Specifically, it will refuse to install whichever package goes on second, saying:

WARN/PackageManager: Can't install because provider name com.xxx.Provider (in package com.xxx) is already used by com.zzz

So.... Don't do that.

There is no way to see if the ContentProvider is running. It is started and stopped automatically by ContentResolver as needed. When you start making requests for a specific contentAuthority, the associated provider will be started if it isn't already running. It will be stopped automatically by ContentResolver, some time later once it has sat idle and it looks like it might not be needed for a while.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top