Service 作为 Android 四大组件之一,有着非常重要的作用,Service 被设计为在后台长时间执行而不需要提供页面的任务。
Service 基础知识
Service 的生命周期如下图所示:
启动/关闭 Service
通过如下代码既可启动 Service。
val intent = Intent(this, SampleService::class.java)
startService(intent)
如果我们期望启动一个前台 Service,对于 Android O 之前的版本可以直接用上面的代码,否则需要使用如下代码:
val intent = Intent(this, SampleService::class.java)
startForegroundService(intent)
简单起见,也可以使用 ContextCompat 中提供的方法:
val intent = Intent(this, SampleService::class.java)
//内部会判断 Android 版本号,来调用不同的方法
ContextCompat.startForegroundService(this, intent)
使用 Intent 参数调用 startService 或者 startForegroundService 方法后,如果服务是未启动状态则会启动该服务,然后会将该 Intent 作为参数回调 Service 中的 onStartCommand 方法。
所以不管服务是启动还是未启动状态,只要我们调用了启动服务的方法,onStartCommand 方法都会被回调,且参数就是我们启动服务时传的 Intent。
onStartCommand 方法返回值为一个整形,用于描述系统应如何在系统终止服务的情况下继续运行服务。返回值必须为如下几个值之一:
-
START_NOT_STICKY
如果系统在 onStartCommand() 返回后终止服务,则除非有待传递的挂起 Intent,否则系统不会重建服务。这是最安全的选项,可以避免在不必要时以及应用能够轻松重启所有未完成的作业时运行服务。 -
START_STICKY
如果系统在 onStartCommand() 返回后终止服务,则其会重建服务并调用 onStartCommand(),但不会重新传递最后一个 Intent。相反,除非有挂起 Intent 要启动服务,否则系统会调用包含空 Intent 的 onStartCommand()。在此情况下,系统会传递这些 Intent。此常量适用于不执行命令、但无限期运行并等待作业的媒体播放器(或类似服务)。 -
START_REDELIVER_INTENT
如果系统在 onStartCommand() 返回后终止服务,则其会重建服务,并通过传递给服务的最后一个 Intent 调用 onStartCommand()。所有挂起 Intent 均依次传递。此常量适用于主动执行应立即恢复的作业(例如下载文件)的服务。
在服务使用完成后,我们需要关闭服务释放资源,在 Activity 中使用 stopService 来关闭服务,在 Service 内部使用 stopSelf 关闭。
//关闭 SampleService 服务
stopService(Intent(this, SampleService::class.java))