You can get ServiceContext using following code in servlet: ServiceContext serviceContext = new ServiceContext(); serviceContext. setScopeGroupId(myGroupId);
Services can be started with Context. startService() and Context. bindService() . Note that services, like other application objects, run in the main thread of their hosting process.
You can go for getApplicationContext() if you wanna get context of whole application. If you want to get context of current class you can use getBaseContext() instead.
Service is a Context
Service
extends ContextWrapper
which extends Context
. Hence the Service
is a Context
.
Use 'this'
keyword in the service.
Service
extends ContextWrapper
ContextWrapper
extends Context
So....
Context context = this;
(in Service or Activity Class)
Since Service
is a Context
, the variable context must be this
:
DataBaseManager dbm = Utils.getDataManager(this);
just in case someone is getting NullPointerException
, you need to get the context inside onCreate().
Service
is a Context
, so do this:
@Override
public void onCreate() {
super.onCreate();
context = this;
}
As Service is already a Context itself
you can even get it through:
Context mContext = this;
OR
Context mContext = [class name].this; //[] only specify the class name
// mContext = JobServiceSchedule.this;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With