4

I want to access the database from my service which runs in the background. Is that possible?

xyzandroid
  • 139
  • 1
  • 3
  • 9

4 Answers4

3

Yes, it is possible to access your database from service. To access database, all you need is Context which is also available in service.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
waqaslam
  • 67,549
  • 16
  • 165
  • 178
1

Yes it is possible to access the database from a service. All you have to do is, create an object of the database class that extends SqliteOpenHelper, pass the ServiceName.this as the context or else use your application class as the context.

DbHelper db = new DbHelper(ServiceClass.this);
db.performOperations();
db.close();

OR

DbHelper db = new DbHelper(YourApplicationClass.getAppContext());
db.performOperations();
db.close();
0

Yes, its possible, and whereever is a need of context use service's.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

Yes, this is a normal functionality which we Use in application. @Ajay is right.

USE EXAMPLE

You can Call a method that access DB from your service class in thread which execute after some seconds.

Community
  • 1
  • 1
Siten
  • 4,515
  • 9
  • 39
  • 64