Android的服务多个实例 [英] Android Service multiple instances

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

问题描述

林还是有点新的Andr​​oid的服务类。我知道你需要从startService(意向)申请启动该服务,但我的问题是我的服务里面的方法。我需要有一个意图启动该服务,然后在我的活动创建一个类的对象,所以我可以调用该服务的方法。问题是,当我这样做我创建服务的一个实例,当我与意图和服务的另一个实例启动它,当我在我的活动创建该类的对象。这意味着传递到服务,从startService(意向)的任何数据是不存在的,当我创建的服务对象。解决这个任何方式还是我只是完全滥用服务类?我可以提供一些code,但它基本上是这样的:

  //创建ControlPanel控制服务类的对象。
ControlPanel控制的cPanel =新ControlPanel控制();
//启动ControlPanel控制服务类创建意图
意图ControlPanel控制=新意图(这一点,cPanel.getClass());
//开始服务
startService(控制面板);


解决方案

我说你是滥用类: - )

调用startService()多次不会导致启动多个服务。

从<一个href=\"http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29\"相对=nofollow> DOC :


  

这是一个给定的应用服务启动请求。这样做的目的可以包含特定服务实现的完整的类名来启动,或通过动作和各种服务的其他领域的抽象定义开始。如果此服务尚未运行,它将被实例化和启动(如果需要创建用于其的方法);如果正在运行那么它仍然保持运行。


您应该重写<一个href=\"http://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29\"相对=nofollow> onStartCommand()为好。如果它尚未启动的第一个startService呼叫启动服务。在任何情况下onStartCommand将拦截要发送给它的任何进一步startService调用和意图。

Im still a bit new to the Android Service Class. I know you need to start the service from your application with startService(intent), however my problem is my service has methods inside it. I need to start the service with an intent and then create an object of that class in my Activity so I can call methods of the service. The problem is when I do this I create one instance of the service when I start it with an intent and another instance of the service when I create an object of the class in my activity. This means any data passed to the service from startService(intent) is not there when I create the object of the service. Any ways around this or am I just totally misusing the service class? I can give some code but its basically this:

//Create Object of ControlPanel service class.
ControlPanel cPanel = new ControlPanel();
//Create intent for starting ControlPanel service class
Intent controlPanel = new Intent(this, cPanel.getClass());
//Start Service
startService(controlPanel);

解决方案

I'd say you are misusing the class :-).

Calling startService() multiple times does not result in starting multiple service.

From the doc:

Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

You should override onStartCommand() as well. The first startService call starts the service if it has not been started yet. In any case onStartCommand will intercept any further startService calls and the intent you want to send to it.

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

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