您的位置:首页 > 移动开发 > Android开发

Android开发中使用Service还是Thread?

2015-07-28 19:46 495 查看

Android开发中使用Service还是Thread?

首先来看看Android官网关于这个问题是怎么说的

Should you use a service or a thread?

A service issimply a component that can run in the background even when the user is notinteracting with your application. Thus, you should create a service only ifthat is what you need.

If you need toperform work outside your main thread, but only while the user is interactingwith your application, then you should probably instead create a new thread andnot a service. For example, if you want to play some music, but only while youractivity
is running, you might create a thread inonCreate(), start running it inonStart(),
then stop it inonStop(). Also consider usingAsyncTask or HandlerThread,
instead of the traditional Threadclass. See the Processes
and Threading document for more information aboutthreads.

Remember that ifyou do use a service, it still runs in your application's main thread bydefault, so you should still create a new thread within the service if itperforms intensive or blocking operations.

总结:使用Service还是Thread?

Service是一个简单的组件,可以在后台运行,即使用户没有与您的应用程序交互。因此,你应该创建一个服务,如果这是你所需要的。如果你需要在主线程外执行工作,但只有当用户与应用程序交互时,你可能会创建一个新的线程,而不是服务。其实Service和Thread可以说没有什么关系,看看这篇文章怎么说的,很有用,我们不是要考虑该用Thread或者该用Service,而是应该为Thread选择合适的生命周期。摘录如下:

首先,需要了解Service的几个特点。

(1)默认情况下,Service其实是运行在主线程中的,如果需要执行复杂耗时的操作,必须在Service中再创建一个Thread来执行任务。

(2)Service的优先级高于后台挂起的Activity,当然,也高于Activity所创建的Thread,因此,系统可能在内存不足的时候优先杀死后台的Activity或者Thread,而不会轻易杀死Service组件,即使被迫杀死Service,也会在资源可用时重启被杀死的Service。

其实,Service和Thread根本就不是一个级别的东西,Service是系统的四大组件之一,Thread只是一个用来执行后台任务的工具类,它可以在Activity中被创建,也可以在Service中被创建。因此,我们其实不应该讨论该使用Service还是Thread,而是应该讨论在什么地方创建Thread。

典型的应用中,它可以在以下三个位置被创建,不同的位置,其生命周期不一样,所以,我们应该根据该Thread的目标生命周期来决定是在Service中创建Thread还是在Activity中创建它。

(1)在Activity中被创建

这种情况下,一般在onCreate时创建,在onDestroy()中销毁,否则,Activity销毁后,Thread是会依然在后台运行着。

这种情况下,Thread的生命周期即为整个Activity的生命周期。所以,在Activity中创建的Thread只适合完成一些依赖Activity本身有关的任务,比如定时更新一下Activity的控件状态等。

核心特点:该Thread的就是为这个Activity服务的,完成这个特定的Activity交代的任务,主动通知该Activity一些消息和事件,Activity销毁后,该Thread也没有存活的意义了。

(2)在Application中被创建

这种情况下,一般自定义Application类,重载onCreate方法,并在其中创建Thread,当然,也会在onTerminate()方法中销毁Thread,否则,如果Thread没有退出的话,即使整个Application退出了,线程依然会在后台运行着。

这种情况下,Thread的生命周期即为整个Application的生命周期。所以,在Application中创建的Thread,可以执行一些整个应用级别的任务,比如定时检查一下网络连接状态等等。

核心特点:该Thread的终极目标是为这个APP的各个Activity服务的,包括完成某个Activity交代的任务,主动通知某个Activity一些消息和事件等,APP退出之后该Thread也没有存活的意义了。

以上这两种情况下,Thread的生命周期都不应该超出整个应用程序的生命周期,也就是,整个APP退出之后,Thread都应该完全退出,这样才不会出现内存泄漏或者僵尸线程。那么,如果你希望整个APP都退出之后依然能运行该Thread,那么就应该把Thread放到Service中去创建和启动了。

(3)在Service中被创建

这是保证最长生命周期的Thread的唯一方式,只要整个Service不退出,Thread就可以一直在后台执行,一般在Service的onCreate()中创建,在onDestroy()中销毁。

所以,在Service中创建的Thread,适合长期执行一些独立于APP的后台任务,比较常见的就是:在Service中保持与服务器端的长连接。

核心特点:该Thread可以为APP提供一些“服务”或者“状态查询”,但该Thread并不需要主动通知APP任何事件,甚至不需要知道APP是谁。

总之,我们不是要考虑该用Thread或者该用Service,而是应该为Thread选择合适的生命周期。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: