Книга: Beginning Android



As with an activity, the main entry point to a content provider is onCreate(). 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 onCreate() 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 sqlite_master table. This is useful for lazy-creating a table your content provider will need.

For example, here is the onCreate() method for Provider, from the ContentProvider/Constants sample application available in the Source Code section of http://apress.com:

public boolean onCreate() {
 db = (new DatabaseHelper(getContext())).getWritableDatabase();
 return(db == null) ? false true;

While that doesn’t seem all that special, the “magic” is in the private DatabaseHelper object, described in the chapter on database access.

Оглавление книги

Оглавление статьи/книги

Генерация: 1.169. Запросов К БД/Cache: 3 / 0
Вверх Вниз