问题
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...
回答1:
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 :)
回答2:
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.
来源:https://stackoverflow.com/questions/6399075/android-service-startservice-is-called-multiple-times-and-causes-value-mixup