转自 http://blog.csdn.net/liuhe688/article/details/6874378html
富貴必從勤苦得,男兒須讀五車書。唐.杜甫《柏學士茅屋》java
做为程序员的咱们,须知富贵是要经过勤苦努力才能获得的,要想在行业内有所建树,就必须刻苦学习和钻研。android
今天咱们来说一下Android中Service的相关内容。程序员
Service在Android中和Activity是属于同一级别上的组件,咱们能够将他们认为是两个好哥们,Activity仪表不凡,迷倒万千少女,常常作一些公众人物角色,而Service一副彪悍的长相,但却身强力壮,经常在后台作一些搬运工的力气活,虽然有些累,但你们都不能失去他。app
下面咱们就围绕Service对其进行全面讲解:异步
1.Service生命周期ide
Service生命周期能够从两种启动Service的模式开始讲起,分别是context.startService()和context.bindService()。学习
(1).startService的启动模式下的生命周期:当咱们首次使用startService启动一个服务时,系统会实例化一个Service实例,依次调用其onCreate和onStartCommand方法,而后进入运行状态,此后,若是再使用startService启动服务时,再也不建立新的服务对象,系统会自动找到刚才建立的Service实例,调用其onStart方法;若是咱们想要停掉一个服务,可以使用stopService方法,此时onDestroy方法会被调用,须要注意的是,无论前面使用了多个次startService,只需一次stopService,便可停掉服务。this
(2).bindService启动模式下的生命周期:在这种模式下,当调用者首次使用bindService绑定一个服务时,系统会实例化一个Service实例,并一次调用其onCreate方法和onBind方法,而后调用者就能够和服务进行交互了,此后,若是再次使用bindService绑定服务,系统不会建立新的Service实例,也不会再调用onBind方法;若是咱们须要解除与这个服务的绑定,可以使用unbindService方法,此时onUnbind方法和onDestroy方法会被调用。spa
两种模式有如下几点不一样之处:startService模式下调用者与服务无必然联系,即便调用者结束了本身的生命周期,只要没有使用stopService方法中止这个服务,服务仍会运行;一般状况下,bindService模式下服务是与调用者生死与共的,在绑定结束以后,一旦调用者被销毁,服务也就当即终止,就像江湖上的一句话:不求同生,希望同死。
值得一提的是,之前咱们在使用startService启动服务时都是习惯重写onStart方法,在Android2.0时系统引进了onStartCommand方法取代onStart方法,为了兼容之前的程序,在onStartCommand方法中其实调用了onStart方法,不过咱们最好是重写onStartCommand方法。
以上两种模式的流程以下图所示:

下面咱们就结合实例来演示一下这两种模式的生命周期过程。咱们新建一个名为service的项目,而后建立一个MyService的服务类,代码以下:
- package com.scott.service;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.util.Log;
-
- public class MyService extends Service {
-
- private static final String TAG = "MyService";
-
- @Override
- public void onCreate() {
- super.onCreate();
- Log.i(TAG, "onCreate called.");
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Log.i(TAG, "onStartCommand called.");
- return super.onStartCommand(intent, flags, startId);
- }
-
- @Override
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- Log.i(TAG, "onStart called.");
- }
-
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "onBind called.");
- return null;
- }
-
- @Override
- public boolean onUnbind(Intent intent) {
- Log.i(TAG, "onUnbind called.");
- return super.onUnbind(intent);
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- Log.i(TAG, "onDestroy called.");
- }
- }
而后再AndroidManifest.xml中配置服务信息,否则这个服务就不会生效,配置以下:
- <service android:name=".MyService">
- <intent-filter>
- <action android:name="android.intent.action.MyService" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- </service>
若是服务只是在本应用中使用,大能够去掉<intent-filter>属性。
服务搭建完成以后,咱们就来关注一下调用者MainActivity,它很简单,只有两个按钮,一个是启动服务,另外一个是中止服务,咱们来看一下他们的点击事件:
- public void start(View view) {
- Intent intent = new Intent(this, MyService.class);
- startService(intent);
- }
-
- public void stop(View view) {
- Intent intent = new Intent(this, MyService.class);
- stopService(intent);
- }
接下来咱们就先点击一次启动按钮,看看都发生了些什么。日志打印结果以下:

固然咱们以为还不过瘾,再点击一次,咱们会发现结果略有不一样:

咱们看到第二次点击时onCreate方法就再也不被调用了,而是直接调用了onStartCommand方法(onStartCommand中又调用了onStart方法)。咱们选择“Settings->Application s->Running services”就会发现咱们刚刚启动的服务:

而后咱们点击中止按钮,试图中止服务,咱们发现以下现象:

咱们会发现onDestroy方法被调用了,此时服务就中止运行了。咱们再次查看“Running services”,就会发现MyService这个服务已全无踪影。
在这个过程当中,onBind方法和onUnbind方法始终没被调用,咱们下面就让这两位show一下本身。
咱们修改一下MainActivity的代码,使其能够能够以bindService的方式启动一个服务,代码以下:
- private ServiceConnection conn = new ServiceConnection() {
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
-
- Log.i(TAG, "onServiceConnected called.");
- }
-
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
-
- public void bind(View view) {
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
-
- public void unbind(View view) {
- unbindService(conn);
- }
在使用bindService绑定服务时,咱们须要一个ServiceConnection表明与服务的链接,它只有两个方法,onServiceConnected和onServiceDisconnected,前者是在操做者在链接一个服务成功时被调用,然后者是在服务崩溃或被杀死致使的链接中断时被调用,而若是咱们本身解除绑定时则不会被调用,因此咱们这里只研究onServiceConnected这个方法。
看样子是能够去绑定一个服务了,其实还不行,由于咱们前面服务中的onBind方法返回值为null,这样是不行的,要想实现绑定操做,必须返回一个实现了IBinder接口类型的实例,该接口描述了与远程对象进行交互的抽象协议,有了它咱们才能与服务进行交互。咱们因而有了这样的代码:
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "onBind called.");
- return new Binder() {};
- }
咱们返回了一个Binder的实例,而这个Binder偏偏是实现了IBinder接口,因此这样就能够实现绑定服务的操做了,一块儿来演示一下。
先点击一下绑定按钮,咱们会发如今MainActivity中打印日志以下:

似的,onServiceConnected方法被调用了,看来绑定链接已经成功了,看看MyService如何:

onCreate方法和onBind方法被调用了,此时服务已进入运行阶段,若是再次点击绑定按钮,onCreate和onBinder并不会再次被调用,这个过程当中它们仅被调用一次。
而后点击解除绑定按钮,咱们会发现MyService打印以下:

能够看到onUnbind方法和onDestroy方法被调用了,此时MyService已被销毁,整个生命周期结束。
另外一方面,当咱们退出MainActivity时,服务也会随之而结束,从这一点上看,MyService能够说是誓死追随着MainActivity。
须要注意的是,在链接中断状态再去作解除绑定操做会引发一个异常,在MainActivity销毁以前没有进行解除绑定也会致使后台出现异常信息,此时咱们就要想办法确保不会出现此类状况,能够这样作:
- private boolean binded;
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- binded = true;
- }
-
- public void unbind(View view) {
- unbindService();
- }
-
- @Override
- protected void onDestroy() {
- super.onDestroy();
- unbindService();
- }
-
- private void unbindService() {
- if (binded) {
- unbindService(conn);
- binded = false;
- }
- }
以上就是bindService的生命周期,正如咱们上面讲的同样,使用bindService启动服务后调用者和服务绑定到了一块儿,当调用者被销毁,服务也当即结终止。
一般状况下是这样的,不过也有特殊状况。当startService和bindService在同一场合下使用时,就会出现稍微不一样的现象。
若是咱们先以startService方式启动服务,而后再用bindService绑定到这个服务,以后使用unbindService解除绑定,此时服务并不会所以而终止,而是继续运行,直到咱们使用stopService来中止这个服务。下面咱们再修改一下代码以验证这个过程。MyService保持不变,咱们只需修改一下MainActivity。MainActivity最新代码以下:
- package com.scott.service;
-
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.util.Log;
- import android.view.View;
-
- public class MainActivity extends Activity {
-
- private static final String TAG = "MainActivity";
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- private ServiceConnection conn = new ServiceConnection() {
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Log.i(TAG, "onServiceConnected called.");
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
-
-
- public void start(View view) {
- Intent intent = new Intent(this, MyService.class);
- startService(intent);
- }
-
-
- public void bind(View view) {
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
-
-
- public void unbind(View view) {
- unbindService(conn);
- }
-
-
- public void stop(View view) {
- Intent intent = new Intent(this, MyService.class);
- stopService(intent);
- }
- }
在MainActivity中包含了四个按钮事件,分别是startService、bindService、unbindService和stopService,咱们逐一地按下,看看都发生了什么。
首先按下启动服务的按钮,MyService打印以下:

恩,意料之中。而后咱们再按下绑定服务的按钮,MyService打印以下:

此时,只有onBind被调用,以后二者就绑定成功。咱们再按下解除绑定的按钮,MyService打印以下:

此时,onUnbind方法方法被调用,注意,此时MyService并无因解除绑定而终止,而是继续运行。也许咱们内心会问,若是屡次按下绑定服务的按钮或重复以上两个步骤,结果如何呢?答案是onBind和onUnbind都不会再被调用了。看不到onBind被调用,是否是没有绑定成功啊,咱们来看一下MainActivity打印信息:

重复按下绑定按钮,几回都绑定成功了。最后咱们按下中止服务的按钮,MyService打印以下:

此时,onDestroy被调用了,此时MyService中止了运行,整个生命周期结束。
以上就是关于MyService生命周期的讲解,下面咱们来介绍一下如何与服务进行通讯。与服务之间的通讯能够分为两种,进程内的通讯和进程间的通讯,前者调用者和服务在同一应用进程内,然后者是分布在不一样应用进程中的。
2.进程内与服务通讯
进程内与服务通讯实际上就是经过bindService的方式与服务绑定,获取到通讯中介Binder实例,而后经过调用这个实例的方法,完成对服务的各类操做。咱们上面也介绍了很多关于bindService的内容,下面咱们就针对实际需求对代码作改动。首先是MyService,代码以下:
- package com.scott.service;
-
- import android.app.Service;
- import android.content.Intent;
- import android.os.Binder;
- import android.os.IBinder;
- import android.util.Log;
-
- public class MyService extends Service {
-
- private static final String TAG = "MyService";
-
- @Override
- public IBinder onBind(Intent intent) {
- Log.i(TAG, "onBind called.");
- return new MyBinder();
- }
-
-
- public class MyBinder extends Binder {
-
-
- public void greet(String name) {
- Log.i(TAG, "hello, " + name);
- }
- }
- }
咱们建立了一个MyBinder的内部类,定义了一个greet方法,在onBind方法中就将这个MyBinder的实例返回,只要调用者获取到这个实例,就能够像拿着游戏手柄同样对服务进行操做。咱们来看一下调用者的代码吧,MainActivity代码以下:
- package com.scott.service;
-
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.ServiceConnection;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.view.View;
-
- public class MainActivity extends Activity {
-
-
- private MyService.MyBinder binder;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
-
- private ServiceConnection conn = new ServiceConnection() {
-
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- binder = (MyService.MyBinder) service;
- binder.greet("scott");
- }
-
- @Override
- public void onServiceDisconnected(ComponentName name) {
- }
- };
-
-
- public void bind(View view) {
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, conn, Context.BIND_AUTO_CREATE);
- }
-
-
- public void unbind(View view) {
- unbindService(conn);
- }
- }
在上面的代码中,咱们是在绑定服务成功时将IBinder类型的service参数强转为MyService.MyBinder类型,获取绑定中介实例,而后调用其greet方法。
操做一下,看看效果如何。先点击绑定服务的按钮,MyService打印以下:

须要注意的是,与服务绑定是一个异步的过程,也就是说,在这一刻咱们绑定服务,下一刻咱们去操做binder对象,也许它还为null,这就容易引发空指针异常,正确的作法是把这些操做放到绑定成功以后,确保万无一失。
以上就是进程内通讯的内容。