仅创建一个 Service 实例 (Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3692915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create only one instance of Service (Android)
提问by Torstein I. B?
How can I make sure that only one instance of Service is created?
如何确保只创建一个 Service 实例?
I have checked some functions with logging (WeatherService is the class who extends Service):
我用日志检查了一些函数(WeatherService 是扩展 Service 的类):
Log.i(TAG, "Start Id:" + WeatherService.this.hashCode());
Log.i(TAG, "End Id:" + WeatherService.this.hashCode());
It gives different hash codes even when I am sure that the same function is running twice (downloading):
即使我确定同一个函数运行了两次(下载),它也会给出不同的哈希码:
09-12 01:00:55.195: INFO/WeatherService(7222): Start Id:1137653208
09-12 01:00:57.235: INFO/WeatherService(7222): Start Id:1137654296
09-12 01:00:59.035: INFO/WeatherService(7222): Start Id:1138806536
09-12 01:01:39.085: INFO/WeatherService(7222): End Id:1137654296
09-12 01:01:39.265: INFO/WeatherService(7222): Start Id:1137654296
09-12 01:02:22.175: INFO/WeatherService(7222): End Id:1137653208
09-12 01:02:24.815: INFO/WeatherService(7222): End Id:1138806536
09-12 01:02:24.836: INFO/WeatherService(7222): Start Id:1138806536
09-12 01:02:40.275: INFO/WeatherService(7222): End Id:1137654296
I am binding a Activity to the service with:
我将 Activity 绑定到服务:
bindService(new Intent(getApplicationContext(), WeatherService.class)
,mServiceConnection, BIND_AUTO_CREATE);
And the service can run for minutes until it is completed, therefore the service can be binded to/created by many Activities
并且该服务可以运行几分钟直到完成,因此该服务可以被许多活动绑定到/创建
采纳答案by CommonsWare
How can I make sure that only one instance of Service is created?
如何确保只创建一个 Service 实例?
There can only be one instance of a given Service
.
给定的 只能有一个实例Service
。
It gives different hash codes even when I am sure that the same function is running twice (downloading).
即使我确定同一个函数运行了两次(下载),它也会给出不同的哈希码。
Then this
is not the Service
. Or, the service had been destroyed and recreated between logs.
然后this
不是Service
。或者,服务已被销毁并在日志之间重新创建。
And the service can run for minutes until it is completed, therefore the service can be binded to/created by many Activities
并且该服务可以运行几分钟直到完成,因此该服务可以被许多活动绑定到/创建
Then the Service
is probably being destroyed and recreated. If you need the service to run for minutes, you need to use startService()
and stopSelf()
in addition to your bindService()
and unbindService()
calls. Or, perhaps you do not need to bind at all, in which case you might consider using an IntentService
, since that automatically gives you a background thread on which to do your downloads.
然后Service
可能正在被破坏和重建。如果您需要该服务运行几分钟,除了您的和呼叫之外,您还需要使用startService()
和。或者,也许您根本不需要绑定,在这种情况下,您可以考虑使用,因为这会自动为您提供一个后台线程来进行下载。stopSelf()
bindService()
unbindService()
IntentService
回答by Hogun
I experienced situation similar.
If you have been written as follow, instance can be created over one .
我遇到过类似的情况。
如果你已经写成如下,实例可以创建一个。
bindService(new Intent(getApplicationContext(), WeatherService.class), mServiceConnection, BIND_AUTO_CREATE);
Try to write once as follows.
尝试写一次如下。
getApplicationContext().bindService(new Intent(getApplicationContext(), WeatherService.class),mServiceConnection, BIND_AUTO_CREATE);