1

I am using the WakefulIntentService 3rd party IntentService implementation.

Basically I have a few planned tasks and I can only run them here:

protected void doWakefulWork(Intent intent) {
    System.out.println("This is where I do the work for every 10 secs!"); 
}

However this only passes intent, what I want to do is able to use Context for various reasons such as getSystemService, openOrCreateDatabase etc. which are available within a Context. So how can I access context from an intent? If this is not possible, how can I do tasks like connecting to a database?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103

2 Answers2

2

An IntentService is-a Service and a Service is-a Context. So just use YourIntentService.this where a Context is needed.

laalto
  • 150,114
  • 66
  • 286
  • 303
2

context is a reference to current object . Use YourServiceName.this. As every Service is a context as well.

Umer Farooq
  • 7,356
  • 7
  • 42
  • 67