0

I have two services in my application - lA_svc & lB_svc.

The MainActivity lauches lA_svc - which context should I use for launching the service from the activity?

Now lA_svc sends an intent to launch lB_svc - which context should I use here in the startService() method?

After a while, lB_svc sends an intent to launch lA_svc, which context do I use here?

Can you please link any gist(s), example(s) to understand the context as per which the services are to be launched?

QHarr
  • 83,427
  • 12
  • 54
  • 101

2 Answers2

0

which context should I use for launching the service from the activity?

A Service can be started from every component. Since you are in an Activity I assume that you have access to two Context:

  • Activity context
  • Application context

It doesn't matter which one you use, but it makes more sense to me, to use the one of your enclosing component, so the Activity context (use this).

Now lA_svc sends an intent to launch lB_svc. - which context should I use here in the startService() method?

Again, you are allowed to start a service from every components, and again it makes sense to use the Context of your enclosing component, so Service context (just use this in your service).

After a while, lB_svc sends an intent to launch lA_svc, which context do I use here?

The problem here is the same: you want to start a Service from another Service. So just use your Service context.

The best article I have ever read about Context is this from Dave Smith.

GVillani82
  • 17,196
  • 30
  • 105
  • 172
0

Basically both your Activity and Services are all context. While starting a service, it doesn't matter which context you use. Use you Activity/Service or even application context.

Here's a nice article that specifies when actually it matters which context to use.

elmorabea
  • 3,243
  • 1
  • 14
  • 20