创建服务只有一个实例(安卓) [英] Create only one instance of Service (Android)

查看:144
本文介绍了创建服务只有一个实例(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能确保创建服务只有一个实例?

我也查了一些功能,以便记录(的WeatherService是谁延伸服务类):

  Log.i(TAG,开始编号+ WeatherService.this.hash code());
Log.i(TAG,结束ID:+ WeatherService.this.hash code());
 

它给不同的hash codeS,即使我相信同样的功能运行两次(下载):

 一月九日至十二日:00:55.195:信息/的WeatherService(7222):开始编号:1137653208
一月九日至12日:00:57.235:信息/的WeatherService(7222):开始编号:1137654296
一月九日至12日:00:59.035:信息/的WeatherService(7222):开始编号:1138806536
一月九日至12日:01:39.085:信息/的WeatherService(7222):结束ID:1137654296
一月九日至12日:01:39.265:信息/的WeatherService(7222):开始编号:1137654296
一月九日至12日:02:22.175:信息/的WeatherService(7222):结束ID:1137653208
一月九日至12日:02:24.815:信息/的WeatherService(7222):结束ID:1138806536
一月九日至12日:02:24.836:信息/的WeatherService(7222):开始编号:1138806536
一月九日至12日:02:40.275:信息/的WeatherService(7222):结束ID:1137654296
 

我绑定一个活动的服务与

  bindService(新意图(getApplicationContext(),WeatherService.class)
                               ,mServiceConnection,BIND_AUTO_CREATE);
 

和服务能为分钟运行,直到它完成,因此该服务可以绑定到/许多活动创建

解决方案
  

我如何才能确保创建服务只有一个实例?

只能有一个给定的服务的一个实例

  

它给不同的hash codeS,即使我相信同样的功能运行两次(下载)。

然后不是服务。或者,该服务已被摧毁和日志之间重新建立。

  

和服务能为分钟运行,直到它完成,因此该服务可以绑定到/创建   许多活动

然后服务很可能被破坏并重新创建。如果您需要的服务运行几分钟,你需要使用 startService() stopSelf()除你的 bindService() unbindService()通话。或者,也许你并不需要绑定的,在这种情况下,你可能会考虑使用 IntentService ,因为这会自动让你在其中做你下载一个后台线程。

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

解决方案

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.

这篇关于创建服务只有一个实例(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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