Skip to content Skip to sidebar Skip to footer

Android Calling Intentservice Class From A Service Class

Can we call IntentService class from a running Service class . Intent myintentservice = new Intent(this, MyIntentService.class); startService(myintentservice); I using above line

Solution 1:

I have answered another question today which has exactly the same mistake.

ServiceAobj1=newServiceA();
ob1.IntService();

You are trying to create a new object of a service which is a very very bad practice. You cannot simply create an object of service like this. All Services in Android must go through the Service lifecycle so that they have a valid context attached to them. In this case a valid context is not attached to the obj1 instance. So as a result the line

Intent MyIntentService = newIntent(this, MyIntentService.class);

causes a null pointer as 'this' is null. (Why? Because this refers to the context which has not yet been created as the service is not started using startService(intent))

Btw I don't understand why you are starting the intent service from within the service. you can simply do it from the DataProviderObserver class like this

Intent MyIntentService = newIntent(context, MyIntentService.class);
context.startService(MyIntentService);

since context is present in the class.

Post a Comment for "Android Calling Intentservice Class From A Service Class"