Android应用程序产卵线程与服务之间的区别? [英] Difference between Android application spawning thread vs. Service?

查看:153
本文介绍了Android应用程序产卵线程与服务之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,有一个必要的背景和一个单独的线程执行的工作。对于我的第一个证明了概念我的子类的应用类里面的onCreate(),我产生一个线程,做后台工作。这工作好了。然而,我才意识到,在过去,我已经使用了服务这样的情况。

I have an Android application that has a need to perform work in the background and on a separate thread. For my first proof-of-concept I subclassed the Application class and inside onCreate() I spawn a Thread that does the background work. This works just great. However, I just realized that in the past I've used a service for situations like this.

现在的问题是,有没有理由不这样做从一个服务,而不是()由Application.onCreate催生了一个线程催生一个线程的工作?该服务是应该执行的背景的工作(它使用的UI线程,除非线程时,我知道),是独立活动的,可以运行,而没有活动可见。使用基于应用程序的线程似乎完成这一切都一样好。如果不使用服务,它实际上消除复杂性,因为该活动刚刚访问应用单。据我知道我有没有必要绑定到服务。

The question is, is there a reason to do work on a Thread spawned from a Service instead of a Thread spawned by Application.onCreate()? The Service is supposed to perform "background" work (it uses the UI thread unless a Thread is used, I know) that is independent of the Activity and can run while no Activity is visible. Using an Application-based thread seems to accomplish all this just as well. By not using a Service it actually removes complexity because the Activity just accesses the Application singleton. As far as I know I have no need to bind to the Service.

我会得到的点点生命周期的极端情况,使用服务将prevent?这是唯一的问题我有过这种方法,但除此之外,我没有对服务的好处卖。

Will I get bit by lifecycle corner cases that using a Service would prevent? That's the only concern I have over this approach, but otherwise I'm not sold on the benefits of a Service.

推荐答案

所不同的是,如果你想线程在后台活动运行,只有当运行或者,如果你想让它继续在用户离开时运行。

The difference would be if you want the thread to run in the background only when the Activity is running or if you want it to continue to run when the user leaves.

服务能够在后台运行,即使当活动不再可用。它们旨在用于当你的应用应当继续进行工作,而不在不久的将来任何用户的参与。如果您运行的服务,该线程将继续在用户离开应用程序,即使运行。这可能是有益的,有时因为用户可能希望你能保持在下载一个真正的大文件,但不希望应用程序继续在前台运行。然后,几个小时(日,月,年)后,用户可以重新进入应用程序读取该文件。

Services are capable of running in the background even when the Activity is no longer available. They are intended to be used when your app should continue to do work without any user involvement in the near future. If you run the Thread in the Service, the thread will continue to run even when the user leaves the app. This can be beneficial sometimes as the user may want you to keep downloading a really large file but doesn't want the app to continue to run in the foreground. Then, a few hours (days, months, years) later the user can re-enter the app to read the file.

然而,如果你使用一个线程,需要不断更新基于结果的用户界面,它可能会更有利,因为它没有真正的目的在一个服务运行活动中启动它。这也可能是在你的程序更容易为你的线程进行通话的用户界面,如果是在活动,而不是服务。 (可能有一些性能优势由于Android没有在它上面的列表处理另一个服务,但是这是纯属炒作对我而言,我没有证明它。)

If, however, you're using a thread that needs to constantly update the UI based on results, it may be more beneficial to launch it within the Activity since it has no real purpose to run in a Service. It also may be easier in your program for your Thread to talk to the UI if it's in the Activity rather than the Service. (There may be some performance benefits as Android doesn't have to handle yet another Service on it's list, but that's purely speculation on my part. I have no proof of it.)

注:在活动创建的线程仍然会继续运行,即使在活动退出。然而,这仅仅是因为该应用仍然在存储器中。该活动与它的线程是在一个更高的优先级从内存比服务线程活动时不再在视图中删除。

NOTE: Threads created in Activities will still continue to run even when the Activity quits. However, this is merely because the app is still in memory. The Activity and it's thread are on a higher priority to be deleted from memory than a Service thread when the Activity is no longer within view.

这篇关于Android应用程序产卵线程与服务之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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