仅创建一个Service实例(Android)

发布于 2024-09-18 17:19:55 字数 1136 浏览 2 评论 0原文

如何确保只创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生一个溪 2024-09-25 17:19:55

如何确保只创建一个 Service 实例?

给定的Service 只能有一个实例。

即使我确定同一函数运行两次(下载),它也会给出不同的哈希代码。

那么this就不是Service。或者,服务已被销毁并在日志之间重新创建。

服务可以运行几分钟直到完成,因此可以绑定/创建服务
通过许多活动

那么 Service 可能会被销毁并重新创建。如果您需要服务运行几分钟,除了 bindService()unbindService() 调用。或者,也许您根本不需要绑定,在这种情况下您可能会考虑使用 IntentService,因为它会自动为您提供一个用于执行下载的后台线程。

How can I make sure that only one instance of Service is created?

There can only be one instance of a given 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.

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.

绅士风度i 2024-09-25 17:19:55

我经历过类似的情况。
如果你已经写成如下,实例就可以创建一个以上。

bindService(new Intent(getApplicationContext(),  WeatherService.class), mServiceConnection, BIND_AUTO_CREATE);

尝试按如下方式写一次。

getApplicationContext().bindService(new Intent(getApplicationContext(), WeatherService.class),mServiceConnection, BIND_AUTO_CREATE);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文