Possible Duplicate:
Create SQLite DB in Android library
I am making a Java library and I need an SQLite database to be opened or created at runtime for my library to store data etc…
All the SQLite API for Android I’ve seen requires a context, from an activity to create a DB,since I’m doing a library, without activity etc… how can I get an SQLite DB ?
The problem is my library has only one public class, and inside my library in my inner workings I need to create a SQLite DB for private use.
So I can’t just ask the user to send in a context. Or can I ?
Just see for how does Google do it in their library ?
At no point when I’m using the analytics library for Android do I pass anything other then an app id for analytics. And In their documentation you can read: “Pageviews, events and Ecommerce hits are stored in an SQLite database and dispatched to the Google Analytics servers periodically
Have the consumer of your library supply you with a
Context.Yes, you can. If you notice, many classes in the
android.*packages have methods that take aContextas a parameter. You, too, can have methods that take aContextas a parameter.Now, you will need to document your intended use of the
Contextand the lifespan of the objects that will touch it, so consumers of your library can pass in an appropriate one. For example, if your library will be creating a static data member that holds onto aSQLiteOpenHelper, you will want to actually have the consumer of the library supply you with anApplication, not anActivity,Service, or otherContext, so you do not run into garbage collection issues.