在Android中将Service用作单例 [英] Using Service as singleton in Android
问题描述
创建一个单例工作的Service
是不好的做法吗?我的意思是一个永不停止的Service
,其中包含一些其他引擎和Activities
将使用的私有数据,因此Service
可能具有以下内容:
Is it a bad practice to create a Service
that works as a singleton? I mean a Service
that is never stopped and that contains some private data that some other engines and Activities
would use, so the Service
could have something like:
public class CustomService extends Service {
private List<Profile> mProfiles;
private static CustomService instance;
public static CustomService getInstance() {
if(instance == null) {
instance = new CustomService();
}
return instance;
}
public List<Profile> getProfiles() {
return mProfiles;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
...
}
...
}
执行Service
而不是仅执行单例操作的原因是,它必须独立于应用程序工作,因为在启动时,它连接了一个永远不应该关闭且不依赖于应用程序的websocket.你会建议我做什么?有没有更好的方法可以重用Service
以便从其他引擎和Activities
获得一些数据(例如mProfiles
数组)?
The reason of doing a Service
instead of just a singleton is that it must work independently of the application, as when it starts it connects a websocket that should never be closed and do not depend on the application. What would you suggest me to do? Is there a better way to re-use the Service
in order to have some data (for instance the mProfiles
array) available from other engines and Activities
?
我读过某个地方Service
作为单例,但是我不知道如何从应用程序的任何其他位置访问私有变量.
I've read somewhere that a Service
works as a singleton, but I don't know how to access the private variables from any other point in application.
推荐答案
创建作为单例的服务是否是错误的做法?
Is it a bad practice to create a service that works as a singleton?
这是多余的,并且按照您的建议,从Android
Service
的角度来看,它不能作为要针对其执行功能的应用程序组件
It is redundant and, in the way you suggest, not going to work from the point of view of Android
Service
as application component which is to perform its functionality with respect to
-
反过来受
-
Application
生命周期影响 - 系统事件.
Application
lifecycle that, in its turn, affected by- system events.
这种应用组件的意图是通过
Such an intention of an application component is reached by
- 其在
AndroidManifest.xml
中的声明, - 触发(启动/绑定/注册)并
- (由系统创建)
Application
中组件的实例,并将其附加"到ApplicationThread
/ActivityThread
.
- its declaration in the
AndroidManifest.xml
, - triggering it (starting / binding / registering) and
- creating (by the system) an instance of the component within
Application
and "attaching" it toApplicationThread
/ActivityThread
.
也就是说,应用程序组件与操作系统进程托管的Application
实例相关,并且无法独立运行.
That said, brings to the fact that application components are tied to the Application
instance hosted by OS process and can't run independently.
关于您的方法,有两种情况:
1..根据模式, CustomService
的默认构造函数是私有的.
通过调用getInstance()
来创建CustomService
的单个实例.该实例只是一个Java
对象(单例),与Android
Service
应用程序组件没有任何共同之处. onStart()
,onStartCommand()
等方法将永远不会被系统调用.
By calling getInstance()
a single instance of CustomService
is created. The instance is just a Java
object (singleton) that has nothing in common with Android
Service
application component. The onStart()
, onStartCommand()
etc. methods will never be called by the system.
尝试使用 startService(Intent)
失败,并显示IllegalAccessException: access to constructor not allowed
.
2.. CustomService
的默认构造函数是public (根据发布的代码).
2. CustomService
's default constructor is public (as per the code posted).
如果在AndroidManifest
中声明了服务,并且默认构造函数为空,则startService()
不会失败,但是getInstance()
将创建另一个CustomService
实例,该实例不会被视为Android
Service
应用程序组件.
If the service is declared in AndroidManifest
and the default constructor is empty, startService()
won't fail, however getInstance()
will create another instance of CustomService
that won't be treated as Android
Service
application component.
这不是单身人士.
您建议我做什么?是否有更好的方法来重用该服务,以便从其他引擎和活动中获得一些数据(例如mProfiles数组)?
What would you suggest me to do? Is there a better way to re-use the service in order to have some data (for instance the mProfiles array) available from other engines and activities?
根据文档使用Service
,并选择交流方式您需要:
Use Service
as per the documentation and pick the kind of communication you need:
- 单向通信(
Activity
-->
Service
)-使用onStartCommand(); 中的reference/android/os/Parcelable.html"rel =" noreferrer> - 双向通信(
Activity
<->
Service
)-使用
Parcelable
)
- One-way communication (
Activity
-->
Service
) - use a startedService
and handle eachIntent
(with your data attached to it, likemProfiles
in caseProfile
class implementsParcelable
) inonStartCommand()
; - Two-way communication (
Activity
<->
Service
) - use a boundService
and communicate viaIBinder
.
最后,Android
中的Service
是单例.系统中每个服务只有一个实例.它按需启动,并处理所有待处理的Intent
/绑定客户端.一旦完成或明确停止,它将被销毁.
Finally, Service
in Android
is a singleton. There is only one instance of each service in the system. It starts on demand and handles all pending Intent
s / bound clients. Once it's done or explicitly stopped, it will be destroyed.
这篇关于在Android中将Service用作单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!