简体   繁体   English

服务和单例实例之间的区别?

[英]Differences between a service and a singleton instance?

I'm developing an android application, and would like to know the difference between a service started with startService() and a singleton class performing the same code I put in startService() . 我正在开发一个android应用程序,并且想知道以startService()开始的服务与执行与我在startService()代码相同的单例类之间的区别。

So, for example if I have a VideoRecordingService service set to record a video from the camera on it's start, and a CameraRecorderClass singleton class which have a StartRecording() method that also records a video from the camera, how do they differ? 因此,例如,如果我有一个VideoRecordingService服务设置为在开始时记录来自摄像机的视频,并且有一个CameraRecorderClass单例类,它们具有一个StartRecording()方法,该方法也可以记录来自摄像机的视频,它们之间有何不同? They both non-related to any activity lifecycle, and they both use the main thread to do it's work. 它们都与任何活动生命周期都不相关,并且都使用主线程来完成工作。

Thanks 谢谢

Service is mainly used when you want to do some background operation. 服务主要在您要进行一些后台操作时使用。 For eg:- Playing music in your application. 例如:-在您的应用程序中播放音乐。 So, if you don't have any Activity running you can play music using Service. 因此,如果您没有正在运行的“活动”,则可以使用“服务”播放音乐。

While your Singleton instance would not be working if you close your application/activity if unless you are performing it in some background task. 如果您关闭应用程序/活动,则Singleton实例将无法正常运行,除非您在某些后台任务中执行它。 Also, Service will restart automatically if you return START_STICKY from onStartCommand when your Service is killed due to some reason. 另外,如果由于某种原因而onStartCommand服务时,如果您从onStartCommand返回START_STICKY ,则服务将自动重新启动。

So, in your case if you really want to do some long background running operation then its better to use Service instead of your Singleton instance. 因此,在您的情况下,如果您真的想进行长时间的后台运行操作,则最好使用Service而不是Singleton实例。

When using startService it creates a new instance of that class, it can have a context and do a wide range of things that the Service class inherits. 使用startService它将创建该类的新实例,它可以具有上下文,并可以执行Service类继承的各种操作。 You can create this anywhere in your application where you have a context, and you can start and stop it multiple times (using startService and stopSelf ) 您可以在具有上下文的应用程序中的任何位置创建此代码,并且可以多次启动和停止它(使用startServicestopSelf

With a singleton class, well, its a static object that you can only have once instance of (unless you want to create more I guess?). 好吧,对于一个单例类,它是一个静态对象,您只能有一个实例(除非您想创建更多实例,否则)。 The static object can isn't much different, however it doesn't have a context and all that nice android stuff that comes with a class (unless you pass it a context or what ever you may need). 静态对象可以没有太大的区别,但是它没有上下文以及类附带的所有出色的Android东西(除非您将其传递给上下文或您可能需要的任何东西)。

A service can also be run without needing to invoke it by using a activity, or showing a UI, it can run in the background with no UI, and can be started using a broadcast listener without interrupting the user, as long as the service is running then the service shouldn't be automatically closed by the system, rather then if you started an async task in the singleton and then closed the activity and the activity was destroyed. 服务也可以运行而无需通过使用活动或显示UI来调用,它可以在没有UI的情况下在后台运行,并且可以使用广播侦听器启动而无需中断用户,只要该服务是运行该服务不应被系统自动关闭,而是如果您在单例中启动了异步任务,然后关闭了活动,并且活动被销毁了。

There may be more to it. 可能还有更多。 But you would have to look into dalvik.. 但您必须研究达尔维克..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM