Service是Android中四大组件之一,在Android开发中起到很是重要的做用,它运行在后台,不与用户进行交互。java
一、Service的继承关系:android
java.lang.Object → android.content.Context → android.content.ContextWrapper → android.app.Service网络
二、Sevice定义:app
Service(服务)是一个没有用户界面的在后台运行执行耗时操做的应用组件。其余应用组件可以启动Service,而且当用户切换到另外的应用场景,Service将持续在后台运行。另外,一个组件可以绑定到一个service与之交互(IPC机制),例如,一个service可能会处理网络操做,播放音乐,操做文件I/O或者与内容提供者(content provider)交互,全部这些活动都是在后台进行。异步
三、Service有两种状态," 启动的 " 和 " 绑定 ":ide
[1]经过startService()启动的服务处于" 启动的 "状态,一旦启动,service就在后台运行,即便启动它的应用组件已经被销毁了。一般started状态的service执行的任务而且不返回任何结果给启动者。好比当下载或上传一个文件,当这项操做完成时,service应该中止它自己。函数
[2]" 绑定 "状态:经过调用bindService()来启动,一个绑定的service提供一个容许组件与service交互的接口,能够发送请求、获取返回结果,还能够经过夸进程通讯来交互(IPC)。绑定的service只有当应用组件绑定后才能运行,多个组件能够绑定一个service,当调用unbind()方法时,这个service就会被销毁了。this
注意:默认状况下,service与activity同样都存在与当前进程的主线程中,因此,一些阻塞UI的操做,好比耗时操做不能放在service里进行,好比另外开启一个线程来处理诸如网络请求的耗时操做。若是在service里进行一些耗CPU和耗时操做,可能会引起ANR警告,这时应用会弹出是强制关闭仍是等待的对话框。因此,对service的理解就是和activity平级的,只不过是看不见的,在后台运行的一个组件,这也是为何和activity同被说为Android的基本组件。启动后的Service具备较高的优先级,通常状况下,系统会保证Service的正常运行,只有当前台的Activity正常运行的资源被Service占用的状况下,系统才会中止Service;当系统从新得到资源后,会根据程序的设置来决定是否从新启动原来的Service。spa
四、Service的生命周期:线程
注意:bind service的不一样之处在于当绑定的组件销毁后,对应的service也就被kill了。
service生命周期也涉及一些回调方法,这些方法都不用调用父类方法,具体以下:
public class ExampleService extends Service { int mStartMode; // indicates how to behave if the service is killed IBinder mBinder; // interface for clients that bind boolean mAllowRebind; // indicates whether onRebind should be used @Override public void onCreate() { // The service is being created } @Override public int onStartCommand(Intent intent, int flags, int startId) { // The service is starting, due to a call to startService() return mStartMode; } @Override public IBinder onBind(Intent intent) { // A client is binding to the service with bindService() return mBinder; } @Override public boolean onUnbind(Intent intent) { // All clients have unbound with unbindService() return mAllowRebind; } @Override public void onRebind(Intent intent) { // A client is binding to the service with bindService(), // after onUnbind() has already been called } @Override public void onDestroy() { // The service is no longer used and is being destroyed } }
五、Service的一个子类:IntentService
IntentService使用队列的方式将请求的Intent加入队列,而后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个以后再处理第二个,每个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,所以,这里提供一个思路,若是有耗时的操做与其在Service里面开启新线程还不如使用IntentService来处理耗时操做。而在通常的继承Service里面若是要进行耗时操做就必须另开线程,可是使用IntentService就能够直接在里面进行耗时操做,由于默认实现了一个worker thread。
实现实例:
public class defaultIntentService extends IntentService { //必须有构造函数, 而且必须带有worker thread的name做为参数调用super IntentService(String) public defaultIntentService() { super("defaultIntentService"); } //经过从默认worker thread调用此带有intent的方法启动service //当方法返回IntentService会适时中止service
@Override protected void onHandleIntent(Intent intent) { // 一般咱们会作一些好比下载文件等的工做. // 好比实例中,睡眠五秒. long endTime = System.currentTimeMillis() + 5*1000; while (System.currentTimeMillis() < endTime) { synchronized (this) { try { wait(endTime - System.currentTimeMillis()); } catch (Exception e) { } } } } }
六、中止Service
[1]若是service是非绑定的,最终当任务完成时,为了节省系统资源,必定要中止service,能够经过stopSelf()来中止,也能够在其余组件中经过stopService()来中止;
[2]绑定的service能够经过onUnBind()来中止service。