OnCreate()
As with an activity, the main entry point to a content provider is . Here you can do whatever initialization you want. In particular, here is where you should lazy‑initialize your data store. For example, if you plan on storing your data in such‑and‑so directory on an SD card, with an XML file serving as a “table of contents,” you should check if that directory and XML file are there and, if not, create them so the rest of your content provider knows they are out there and available for use.
Similarly, if you have rewritten your content provider sufficiently to cause the data store to shift structure, you should check to see what structure you have now and adjust it if what you have is out‑of‑date. You don’t write your own “installer” program and so have no great way of determining if, when is called, this is the first time ever for the content provider, the first time for a new release of a content provider that was upgraded in place, or just a normal startup.
If your content provider uses SQLite for storage, you can detect if your tables exist by querying on the table. This is useful for lazy‑creating a table your content provider will need.
For example, here is the method for Provider, from the sample application available in the Source Code section of http://apress.com:
Дата добавления: 2015-05-16; просмотров: 669;