I developed an app with:
Now i'd like to write a simple widget that just perform the app button action: insert some data in the SAME sqlite db. The goal is to use the widget for fast insert without opening the app, and the app when i need more complex opertions.
Thanks
答案 0 :(得分:3)
is it possible to share the same db between the app (the real owner) and the widget?
Yes.
how?
Usually, the app widget is just part of the app. You use it no differently than you would from an activity or fragment, or anything else, since it is all one app.
Do bear in mind, though, that AppWidgetProvider
methods like onUpdate()
are called on the main application thread. In an activity, you might just use a bare thread to do database I/O, which is OK, as your app is in the foreground. For an app widget update, though, your app may be in the background, and your process may not live very long. Use an IntentService
to do the database I/O (kicked off by onUpdate()
) and update the app widget contents.
do I need a contentProvider?
Only if you want your app widget to be in a separate app from the rest of your app code. Usually, that is not a good idea, as then users have to install two apps (the main app plus the app widget app).