Android的服务 - startService被多次调用,并导致价值的mixup ... [英] Android Service - startService is called multiple times and causes value mixup...

查看:539
本文介绍了Android的服务 - startService被多次调用,并导致价值的mixup ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的code这通过包传递到该服务的不同的数据值开始同服一些有条件的电话。
当我检查了遇到只有一个条件,服务工作正常的所有​​条件。
但是,当2个或更多的条件匹配,这些调用此相同的服务,但在包不同的数据值。
问题是,当这种情况下满足由第一个电话都没有得到更换的第二个条件启动相同的服务发送的值。
所以,服务是错误响应。

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.

有这样的

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

当两个条件都满足,然后打电话给startService的两倍。
但我在第二个条件startService调用第一个条件得到的值。

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

推荐答案

什么是onStartCommand的返回类型?

What is your return type in onStartCommand ?

您应该阅读有关服务的生命周期。
http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle

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

我建议使用IntentService,因为它被设计为处理asyncron任务,它也开始在一个工作线程。
http://developer.android.com/reference/android/app/IntentService.html

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

要真正帮助你,你的服务的code是非常重要的:)

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

这篇关于Android的服务 - startService被多次调用,并导致价值的mixup ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆