Basically, what I did was create an abstract base class for each of my ContentProviders and inherit from it for every application that I want to do, overriding the permissions path. So in my AbstractContentProvider I have:
public AbstractContentProvider() { sURIMatcher.addURI(getAuthority(), BASE_PATH, ITEMS); sURIMatcher.addURI(getAuthority(), BASE_PATH + "/#", ITEM_ID); } protected abstract String getAuthority();
and then in each subclass I:
private static final String AUTHORITY = "my.package.app1.ContentProvider"; @Override protected String getAuthority() { return AUTHORITY; }
In AndroidManifest, I will register them with:
<provider android:name="my.package.app1.ContentProvider" android:authorities="my.package.app1.ContentProvider"> </provider>
Now the trick is that I want to access these content providers in a common (library) code that does not know about the specific classes of the application. To do this, I define String in my strings.xml file, which I override for each application. Then I can use:
Uri.parse(getString(R.string.contentProviderUri))
and in every application the correct ContentProvider is used without any conflicts. Thus, basically, using the tuning mechanism for dependency injection.
koljaTM Oct 18 '12 at 20:44 2012-10-18 20:44
source share