6

I have some conditional calls from my code which starts same service with different data values passed through bundle to that service. When I checked for only one condition met, service works fine for all conditions. But when 2 or more conditions match, these calls this same service but with different data values in bundle. Problem is when this scenario is met the values sent by first call are not getting replaced for second condition to start same service. So service is responding wrongly.

It is like this

if(some cond)
{
    some values in serivce intent bundle.startService(serviceintent1);
}
if(some cond)
{
    some data in intent bundle.startService(serviceintent1);
}

When both conditions are met then call to startService is twice. but I am getting values from first condition in second condition startService call.

Help me in this issue...

Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37
om252345
  • 2,395
  • 4
  • 29
  • 31

2 Answers2

3

What is your return type in onStartCommand ?

You should read about the life cycle of the service. https://developer.android.com/reference/android/app/Service.html#ServiceLifecycle

I would suggest to use IntentService, as it is designed for handling asyncron tasks, it also start in a worker thread. http://developer.android.com/reference/android/app/IntentService.html

To really help you, the code of your service is quite important :)

Tosa
  • 658
  • 5
  • 15
  • This helped a lot; didn't think to look at my return type. Thanks. – midiwriter May 27 '13 at 05:29
  • 1
    I think your first link should be https://developer.android.com/reference/android/app/Service.html#ServiceLifecycle ServiceLifecycle vs ProcessLifecycle – Alon Feb 14 '18 at 23:17
0

Have a look at Bind service to activity in Android the question .. where the difference between startservice and bindservice is discussed.. I guess this could solve your problem.

Community
  • 1
  • 1
success_anil
  • 3,659
  • 3
  • 27
  • 31