My app widget use a database, and it’s preloaded in asset directory which activity copy it to database directory on first run.
But if widget is inserted before that, an exception is thrown as the database doesn’t exist yet.
Is there anyway the widget check for the file and if it doesn’t exist, get it from assets and copy it instead of activity?!?
I really recommend using
SQLiteAssetHelperfor packaging databases in an app. You can then usegetReadableDatabase()/getWriteableDatabase()as normal, andSQLiteAssetHelperwill transparently unpack your database from assets if needed.That being said, if you are sure that you want to do this yourself, call
getAssets()on theContextpassed intoonUpdate()of yourAppWidgetProvider.Note that
onUpdate()of yourAppWidgetProvideris called on the main application thread, so just like you are (hopefully) doing your activity database I/O in a background thread, you should do the same for your app widget. A typical solution for an app widget is to have the work be done by anIntentServicestarted byonUpdate().