Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Service - startService is called multiple times and causes value mixup...

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...

like image 755
om252345 Avatar asked Jun 18 '11 21:06

om252345


1 Answers

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 :)

like image 179
Tosa Avatar answered Nov 11 '22 10:11

Tosa