什么是Service—>Service forms(service 形式)—>Service Basic(servcie基本用法)—>Service communicate Activity(Service 与 Activity 交互)—>Service 与 Thread之间的关系—>IntentService—>Remote Service(远程 Service);
Service define (Service 定义) :
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.(Service是应用程序的一部分,不提供界面并且可以在程序后台一直运行)
A service can essentially take two forms:(Service 两种形式)
1>Started 通过 startService()调用 started service performs a single operation and does not return a result to the caller.it might download or upload a file over the network(特点:通常执行单个操作不会给调用者有返回结果。他主要用于文件的上传和下载文件)。
2>Bound 通过 bindService()调用 A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication(特点:提供像c/s服务,允许组件与service进行交互,可以向service发送请求,组件可以从service获得结果,甚至允许进程之间进行交互。)
3.Caution 注意点
A service runs in the main thread of its hosting process
By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application’s main thread can remain dedicated to user interaction with your activities.(因为Service运行在主线程中,所以要开一个线程执行Service 这样可以减少ANR[程序无响应]或者blocking operations[阻塞进程])
The Basics (Service 基本用法 )
1> .To create a service you must create a subclass of Service (or one of its existing subclasses) (创建service 必须继承Service或者Service的子类)。
2> .The most important callback methods you should override(重写重要的回调方法)。
(1). onStartCommand() :activity, requests that the service be started, by calling startService()。it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService().(Activity调用Started时候需要重写onStartCommand方法,启动Service之后需要停止service 要调用stopSelf()或者stopService()这两个方法去停止 )
(2). onBind() : by calling bindService()。In your implementation of this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder.(当调用Bound时候 ,需要重写 onBind()方法,如果你不需要任何返回的时候 return null。如何你实现这个方法你需要在Activity里面写一个接口与service进行交互 并且返回 IBinder 类型)。只要所有的控件解除绑定 BondService自动停止。(3). onCreate() :The system calls this method when the service is first created, to perform one-time setup procedures (before it calls either onStartCommand() or onBind()). If the service is already running, this method is not called.(当service第一次创建的时候调用,对程序进行初始化 在onStartCommand 或者 onBind 之前就执行的操作。如何service正在后台运行在不在执行改方法。)。 (4). onDestroy() :when the service is no longer used and is being destroyed (当service已经停止使用或者正在被销毁的时候调用这个方法)。
3> .Declaring a service in the manifest (Service和Activity相同需要在mainfest文件中声明一下 注册一下Service);
5.Service与Activity之间的交互
通信实现Service与Activity通信需要实现onBind()这个方法,返回Binder类。不执行onStartCommand方法。
(1)首先在Service里面新增一个MyBinder类继承在Binder类,然后再MyBinder类里面添加一个pulbic void startDownload()方法用于在后台执行任务。在onBind()方法 return MyBinder 类的子类。
(2)其次在Activity里面创建一个和ServiceConnection的类,重写onServiceConnected()方法和onServiceDisconnected()方法,这两个方法分别在Activity与Service建立关联和解除关联的时候调用。在建立关联的方法中,得到MyBinder的实例,这样Activity与Service之间就建立的关系,然后就可以调用MyBinder中的pulbic方法,也就是Activity指挥Service干什么Service就取干什么的功能。
(3)如何把Service与Activity进行绑定,首先构建出一个Intent对象,然后调用bindService()方法将Activity与Service进行绑定。如下需要传人三个参数。 Intent bindIntent = new Intent(this, MyService.class);
bindService(bindIntent, connection, BIND_AUTO_CREATE);
第一个参数刚刚构建的Intent对象,第二个参数是前面创建的ServiceConnection的实例,第三个参数是一个标志位。BIND_AUTO_CREATE表示在Activity与Service建立关联之后自动创建Service。
Service 与 Thread 之间的关系。
Service与Thread虽然都是在后台运行呢没有界面的组件,但是二者没有任何的关系。
Service是运行在主线程中,如果在Service里面编写非常耗时的代码,程序会出现ANR(Application Not Responsing),所以在Service里面开启一个新的线程然后执行耗时操作这样不会出现程序闪退的情况了。
既然在Service里面需要开线程 为什么不在Activity里面直接创建线程呢?
因为Activity很难对Thread进行控制,当Activity被销毁之后,就没有任何其它的办法可以重新获取之前创建的线程的实例。而且在一个Activity中创建的子线程,另一个Activity无法对其进行操作。但是Service就不同了,所有的Activity都可以与Service进行关联,而且操作方便,即使Activity别销毁了,Service仍然存在,之后只要重新与Service建立关联,就能够获取到原有的Service中Binder实例,对其进行操作。IntentService
不用开启线程,之间执行耗时操作。onHandleIntent
IntentService使用队列的方式将请求的Intent加入队列,然后开启一个worker thread(线程)来处理队列中的Intent,对于异步的startService请求,IntentService会处理完成一个之后再处理第二个,每一个请求都会在一个单独的worker thread中处理,不会阻塞应用程序的主线程,这里就给我们提供了一个思路,如果有耗时的操作与其在Service里面开启新线程还不如使用IntentService来处理耗时操作。而在一般的继承Service里面如果要进行耗时操作就必须另开线程,但是使用IntentService就可以直接在里面进行耗时操作,因为默认实现了一个worker thread。对于异步的startService请求,IntentService会处理完成一个之后再处理第二个。RemoteService (远程Service)
将一个普通的Service转化成远程Service,只需要在注册Service的时候将它的android:process属性指定成:remonte就可以了。