Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a sql database from a widget application

Because of some comments, the question in a nutshell:

How can a widget access its own sqlite database?

Why does it not work with a plain DatabaseAdapter class?

Are certain permissions required?

Are there limitations imposed upon a Remotevie/AppWidgetProvider to access its own database (without a ContentProvider)


The question seems simple, but details follow, if someone really wants to know why I am asking:

I have the following setup:

  1. I have a normal android application, which comes with quite a lot of HTML pages in the assets folder. Because of the size of the app, the prefered install location is the external sd card. This works fine.

  2. That means that the app cannot contain a widget, because he widget needs to reside in the internal ROM.

  3. Instead, the widget will read from the app's database on the external sd card. This is achieved through a ContentProvider/ContentProviderClient.

So far that works fine.

Now, that widget needs to maintain some persistent configuration settings, on a per instance basis. Therefore, the AppWidgetProvider class cannot use the preferences, because they would apply on a per class basis.

So, to keep the (quite complex) instance-sepcific configuration data, I want the AppWidgetProvider class to have its own small sqllite database.

Now, while I can access the app's database from the widget via the contentprovider, it seems that the widget cannot access its own database. I have a database adapter class in the widget project; inside the AppWidgetProvider, I have the Context, but no writing seems to happen.

The calls to the database adapter get through OK (I've done some logging), but when I examine the tables, no writing has occurred.

Why is that? Do I really need a ContentProvider inside the widget to access the widget's database? I do not want to go down that road, because when I implemnented the access to the apps database from the widget, I had to go through the ContentProviderClient instead of the ContentProvider. That would be very cumbersome. As I need a bit more than CRUD acccess, that obviously would involve creating a custom ContentProviderClient. The android docs clearly state that "You don't need a provider to use an SQLite database if the use is entirely within your own application." So IMHO, that would mean, that the widget can access its on database directly? Do I need special permissions? I suspect that, because the widgets run as RemoteViews in the homescreen process, access to the db is somehow limited? Any thoughts, ideas, hints on this?

like image 469
mrd Avatar asked Mar 20 '12 14:03

mrd


1 Answers

i suggest to use a service if nothing else seem to work for you . it's a common thing to have a service for the app widget.

like image 167
android developer Avatar answered Oct 22 '22 11:10

android developer