仅创建一个Service实例(Android)
如何确保只创建一个 Service 实例?
我已经检查了一些带有日志记录的函数(WeatherService 是扩展 Service 的类):
Log.i(TAG, "Start Id:" + WeatherService.this.hashCode());
Log.i(TAG, "End Id:" + WeatherService.this.hashCode());
即使我确定同一个函数运行两次(下载),它也会给出不同的哈希代码:
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
我将 Activity 绑定到服务:
bindService(new Intent(getApplicationContext(), WeatherService.class)
,mServiceConnection, BIND_AUTO_CREATE);
并且服务可以运行几分钟直到完成,因此该服务可以绑定到许多活动/由许多活动创建
How can I make sure that only one instance of Service is created?
I have checked some functions with logging (WeatherService is the class who extends 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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定的
Service
只能有一个实例。那么
this
就不是Service
。或者,服务已被销毁并在日志之间重新创建。那么
Service
可能会被销毁并重新创建。如果您需要服务运行几分钟,除了bindService()
和unbindService()
调用。或者,也许您根本不需要绑定,在这种情况下您可能会考虑使用IntentService
,因为它会自动为您提供一个用于执行下载的后台线程。There can only be one instance of a given
Service
.Then
this
is not theService
. Or, the service had been destroyed and recreated between logs.Then the
Service
is probably being destroyed and recreated. If you need the service to run for minutes, you need to usestartService()
andstopSelf()
in addition to yourbindService()
andunbindService()
calls. Or, perhaps you do not need to bind at all, in which case you might consider using anIntentService
, since that automatically gives you a background thread on which to do your downloads.我经历过类似的情况。
如果你已经写成如下,实例就可以创建一个以上。
尝试按如下方式写一次。
I experienced situation similar.
If you have been written as follow, instance can be created over one .
Try to write once as follows.