I have application that uses direct data access to it's Database. I have nothing to expose outside of it for other applications. As CommonsWare says somewhere on SO, "if you have nothing to expose - don't use ContentProvider". Now, I want to add widget to display a couple of strings from one of the tables, storing in Database. Should I refactor the direct access code to the ContentProvider pattern? Is there a way to use direct access to Database from widget? Is there a way to have both ContentProvider and direct access to database in one application?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Now, I want to add widget to display a couple of strings from one of the tables, storing in Database.
I am going to assume that by "widget" you really mean "app widget".
Should I refactor the direct access code to the ContentProvider pattern?
You do not have to do this just because you are writing an app widget.
Is there a way to use direct access to Database from widget?
Sure, using the same mechanisms you are using anywhere else in your app.
Is there a way to have both ContentProvider and direct access to database in one application?
Yes, though you have to be a bit careful to make sure that you are using the same SQLiteDatabase
object in both cases, so you do not run into threading issues.