Service是Android的四大组件之间,可以在后台长期执行任务,但是不提供用户界面,所以我们经常会启动一个Service去执行一些需要在后台执行的任务,但是启动Service有两种方式,一种是直接start去启动,另外一种是通过bind(绑定)来启动的,下面我们就聊一聊这两种启动方式
startService
调用步骤:
1、自定义一个类TestService(类名任意取)去继承Service
2、Android四大组件都是要在Manifest.xml文件中去注册的,因此我们需要在Manifest.xml文件中加上自定的一的TestService
3、在Activity中通过startService(Intent)来开启该服务,在其他的类中也可以通过context.startService(Intent)来调用
4、在Activity中通过stopService(Intent)来停止该服务,在其他的类中也可以通过context.stopService(Intent)来停止,注意一点,服务只可以停止一次
上面的四个步骤就是通过start启动服务到stop停止服务的全过程,接下来我们再看一下这个过程中生命周期是怎样的。
当我们点击一个按钮去启动服务的时候,Log的打印如下,
当我们点击一个按钮去停止服务的时候,Log的打印如下,
从Log的打印情况来看,整个生命周期的过程如下:
onCreate() –> onStartCommand()(onStrat()已经过时) –> onDestroy()
这种方式启动服务有两个特点:
1、调用者在启动该服务之后,两者便没有任何的关系了,以后无论调用者处于什么样的状态,服务还是会在后台执行
2、调用者不能去调用服务里面的方法
bindService
调用步骤:
1、同startService方法
2、同startService方法
3、在Activity中通过bindService(Intent,ServiceConnection,int)来开启该服务
4、在Activity中通过unbindService(mServiceConnection)来停止该服务
上面的四个步骤就是通过start启动服务到stop停止服务的全过程,接下来我们再看一下这个过程中生命周期是怎样的。
当我们点击一个按钮去启动服务的时候,Log的打印如下,
当我们点击一个按钮去停止服务的时候,Log的打印如下,
从Log的打印情况来看,整个生命周期的过程如下:
onCreate() –> onBind() -> onUnbind() –> onDestroy()
并没有像startService中那样调用startCommand()
而且在Log还打印了“我是TestService中的方法“这句话,这句话是在TestService的getString()方法中的,说明在MainActivity中可以调用服务中的方法
总结一下bindService启动服务的两个特点:
1、服务与它的调用者一直存在关系,调用者挂了,那么服务也就是随之挂了
2、调用者可以调用服务中的方法和变量
Demo代码
下面是所有的代码:
TestService
MainActivty