Is it possible to use ContentProvider and direct d

2019-08-28 21:12发布

问题:

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.